Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7836035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:56:05+00:00 2026-06-02T13:56:05+00:00

The Ruby (1.9.3) documentation seems to imply that scan is equivalent to =~ except

  • 0

The Ruby (1.9.3) documentation seems to imply that scan is equivalent to =~ except that

  1. scan returns multiple matches, while =~ returns only the first occurrence, and
  2. scan returns the match data, while =~ returns the index.

However, in the following example, the two methods seem to return different results for the same string and expression. Why is that?

1.9.3p0 :002 > str = "Perl and Python - the two languages"
 => "Perl and Python - the two languages" 
1.9.3p0 :008 > exp = /P(erl|ython)/
 => /P(erl|ython)/ 
1.9.3p0 :009 > str =~ exp
 => 0 
1.9.3p0 :010 > str.scan exp
 => [["erl"], ["ython"]] 

If the index of first match is 0, shouldn’t scan return “Perl” and “Python” instead of “erl” and “python”?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:56:08+00:00Added an answer on June 2, 2026 at 1:56 pm

    When given a regular expression without capturing groups, scan will return an array of strings, where each string represents a match of the regular expression. If you use scan(/P(?:erl|ython)/) (which is the same as your regex except without capturing groups), you’ll get ["Perl", "Python"], which is what you expect.

    However when given a regex with capturing groups, scan will return an array of arrays, where each sub-array contains the captures of a given match. So if you have for example the regex (\w*):(\w*), you’ll get an array of arrays where each sub-array contains two strings: the part before the colon and the part after the colon. And in your example each sub-array contains one string: the part matched by (erl|ython).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ruby 1.9.3 defaults to using Psych for YAML. While the ruby-doc documentation for it
Ruby setters—whether created by (c)attr_accessor or manually—seem to be the only methods that need
The standard Ruby library io/wait provides a method on IO objects ready? that returns
After surfing through tons of documentation on the web it seems that the iPhone
I want to generate the Ruby documentation with hanna template. How to do that?
It seems that I cannot find much documentation on Minitest/spec so I was wondering
This question is similar to Why are methods in Ruby documentation preceded by a
What is the meaning, and where is the Ruby documentation for the syntax of:
Does anybody know some nice documentation about the ruby handsoap gem to get me
I have followed Apple's documentation on localizing iPhone software, using the genstrings ruby script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.