trying regexp in ruby for the first time. the results from the
following code confuses me.
···
-----------------------------------------------------------------------
#!/usr/bin/ruby
require 'B'
def Localmatch(t,s)
myS = Regexp.new(s)
if t =~ myS
puts "'#{s}' matches '#{t}'"
else
puts "'#{s}' does not match '#{t}'"
end
end
myB = B.new
puts "Using a class method to match"
myB.Matchme("The Lord of the Ring", "Ring")
myB.Matchme("No more than one", "King")
puts "Let's try local def"
Localmatch("The Lord of the Ring", "Ring")
Localmatch("No more than one", "King")
class B
def initialize
end
def Matchme(t,stringToMatch)
mystr = Regexp.new(stringToMatch)
if title =! mystr
puts "'#{stringToMatch}' matches '#{t}'"
return true
else
puts "'#{stringToMatch}' does not match '#{t}'"
return false
end
end
end
---------------------------------------------------------------------------
In the class method, "Ring" is not matching the text "The Lord of the
Ring" while in the local def it is. What am i doing wrong?
--
Posted via http://www.ruby-forum.com/.