# Newbie question on regexp in a class method

**URL:** <https://www.ruby-forum.com/t/newbie-question-on-regexp-in-a-class-method/148430>\
**Category:** Ruby\
**Created:** [September 12, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/newbie-question-on-regexp-in-a-class-method/148430 "2008-09-12T19:25:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bellcolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@bellcolt](https://www.ruby-forum.com/u/bellcolt)\
**Post date:** [September 12, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/newbie-question-on-regexp-in-a-class-method/148430/1 "2008-09-12T19:25:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bellcolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@bellcolt](https://www.ruby-forum.com/u/bellcolt)\
**Post date:** [September 12, 2008, 7:38pm UTC](https://www.ruby-forum.com/t/newbie-question-on-regexp-in-a-class-method/148430/2 "2008-09-12T19:38:55Z")

</div>

Rick T. wrote:

> if title =! mystr

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

HTH,  
Sebastian
