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

  • Home
  • SEARCH
  • 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 6800865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:01:24+00:00 2026-05-26T19:01:24+00:00

I am new to Ruby and has always used String.scan to search for the

  • 0

I am new to Ruby and has always used String.scan to search for the first occurrence of a number. It is kind of strange that the returned value is in nested array, but I just go [0][0] for the values I want. (I am sure it has its purpose, just that I haven’t used it yet.)

I just found out that there is a String.match method. And it seems to be more convenient because the returned array is not nested.

Here is an example of the two, first is scan:

>> 'a 1-night stay'.scan(/(a )?(\d*)[- ]night/i).to_a
=> [["a ", "1"]]

then is match

>> 'a 1-night stay'.match(/(a )?(\d*)[- ]night/i).to_a
=> ["a 1-night", "a ", "1"]

I have check the API, but I can’t really differentiate the difference, as both referred to ‘match the pattern’.

This question is, for simply out curiousity, about what scan can do that match can’t, and vise versa. Any specific scenario that only one can accomplish? Is match the inferior of scan?

  • 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-05-26T19:01:25+00:00Added an answer on May 26, 2026 at 7:01 pm

    Short answer: scan will return all matches. This doesn’t make it superior, because if you only want the first match, str.match[2] reads much nicer than str.scan[0][1].

    ruby-1.9.2-p290 :002 > 'a 1-night stay, a 2-night stay'.scan(/(a )?(\d*)[- ]night/i).to_a
     => [["a ", "1"], ["a ", "2"]] 
    ruby-1.9.2-p290 :004 > 'a 1-night stay, a 2-night stay'.match(/(a )?(\d*)[- ]night/i).to_a
     => ["a 1-night", "a ", "1"] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm kind of new to Ruby and some of the closure logic has me
I'm new to RVM. So I have a RVM setup that has ruby 1.9.2
Ruby on Rails has become a new competitive face in the server programming industry,
Ruby 1.9 has a few new syntax elements, such as the {key: value} hash
I'm new to ruby and I'm playing around with the IRB. I found that
Blocking berkeley sockets normally used by Ruby has one downside: if connection with remote
I'm new to ruby and feel that I still do a lot of things
OK, so I'm really new to Ruby on Rails. I'm using InstantRails, if that
I'm very new to ruby. I use IronRuby and my ruby code has long
I'm new to Ruby On Rails. I love, it has Testing capabilities built in.

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.