Ruby On Rails Tutorial - regex

Issues related to building and deploying Ruby on Rails.
no avatar
yertle
 
Posts: 1
Joined: Wed May 26, 2010 9:27 pm

Ruby On Rails Tutorial - regex

by yertle » Wed May 26, 2010 9:32 pm

I stumbled on the regular expressions part of the Ruby Regular Expressions tutorial, and the very first example doesn't make very much sense to me. Here it is:

Example:

#!/usr/bin/ruby

line1 = "Cats are smarter than dogs";
line2 = "Dogs also like meat";

Code: Select all
if ( line1 =~ /Cats(.*)/ )
  puts "Line1 starts with Cats"
end
if ( line2 =~ /Cats(.*)/ )
  puts "Line2 starts with Dogs"
end


This will produce following result:

Line starts with Cats


First of all there's no way that I see to print "Line starts with Cats" - it should be "Line1 starts with Cats"

Second of all, it should be printing "Line2 starts with Dogs" - because line2 =~ /Cats(.*)/

What am I missing? I'm hoping I'm in fact understanding how regular expressions work and that the example has some issues. It's confusing for the first example to potentially be wrong though... Any help would be appreciated, thanks.
Code: Select all

Return to Ruby on Rails