Newbie question on regexp in a class method

R

Rick Tan

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
 
S

Sebastian Hungerecker

Rick said:
if title =! mystr

=! != =~
The above might look like line noise but should tell you everything you need
to know ;-)

HTH,
Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top