Search code examples
ruby-on-railsrubyruby-on-rails-4ruby-on-rails-3.1

method match1.regex seems not working in ruby


I'm following a tutorial on regular expressions in ruby, but a method called regex that seems not working

Tutorial

re = /(\w*)\s(\w*),\s?([\w\s]*)/
match1 = str1.match re
match2 = str2.match re
match1.regex # => wsw,s[ws]     (this is IRB's unique way of showing regular expressions; it will still work normally)

My console

The regex method the method throws an error

1.9.3-p547 :033 > re = /(\w*)\s(\w*),\s?([\w\s]*)/
 => /(\w*)\s(\w*),\s?([\w\s]*)/ 
1.9.3-p547 :034 >     match1 = str1.match re
 => #<MatchData "Joe Schmo, Plumber" 1:"Joe" 2:"Schmo" 3:"Plumber"> 
1.9.3-p547 :035 >     match2 = str2.match re
 => #<MatchData "Stephen Harper, Prime Minister" 1:"Stephen" 2:"Harper" 3:"Prime Minister"> 
1.9.3-p547 :036 > match1.regex
NoMethodError: undefined method `regex' for #<MatchData "Joe Schmo, Plumber" 1:"Joe" 2:"Schmo" 3:"Plumber">
    from (irb):36
    from /home/fernando/.rvm/rubies/ruby-1.9.3-p547/bin/irb:12:in `<main>'
1.9.3-p547 :037 > 

Solution

  • I think it should be

    match1.regexp
    

    with a final 'p'