Newby asks: giving regexps as argument for funvtions

Hi this is a newby question:

i want to pass a regular expression as a parameter to ruby. Tthe following
code snippet can be made easier but it shows the principle I want to
understand:

def findPosRE(str, re)
m = %r{re}.match(str)
return m.begin(0)
end

pos = findPosRE (“hey you behind the wall”, “/behind/”)

This gives the error:

–>undefined method ‘begin’ for nil (NameError)

How I convince ruby that the local variable m is a MatchData object, so the
begin() method can be sent to m ?

Ciao,

Daniel.

i want to pass a regular expression as a parameter to ruby. Tthe following
code snippet can be made easier but it shows the principle I want to
understand:

In your example you give it a String not a Regexp

   def findPosRE(str, re)
      m = re.match(str)
      m.begin(0)
   end

   pos = findPosRE ("hey you behind the wall", /behind/)

but it will give an error if the regexp don't match

def findPosRE(str, re)
    m = %r{re}.match(str)
    return m.begin(0)
end

If you want to give it a String

   def findPosRE(str, re)
      m = %r{#{re}}.match(str)
      m.begin(0)
   end

   pos = findPosRE ("hey you behind the wall", "behind")

Guy Decoux

This worked for me under ruby 1.7.2 (2002-07-02) [i386-mswin32]:

def findPosRE(str, re)
m = re.match(str)
return m.begin(0)
end

pos = findPosRE(“hey you behind the wall”, /behind/)
puts pos # prints 8

“T-Online” daniel.schnell@embeddedware.de wrote in message
news:ap3a59$dm4$06$1@news.t-online.com

Hi this is a newby question:

i want to pass a regular expression as a parameter to ruby. Tthe following
code snippet can be made easier but it shows the principle I want to
understand:

def findPosRE(str, re)
m = %r{re}.match(str)
return m.begin(0)
end

pos = findPosRE (“hey you behind the wall”, “/behind/”)

This gives the error:

–>undefined method ‘begin’ for nil (NameError)

How I convince ruby that the local variable m is a MatchData object, so
the

···

begin() method can be sent to m ?

Ciao,

Daniel.

i want to pass a regular expression as a parameter to ruby. Tthe following
code snippet can be made easier but it shows the principle I want to
understand:

def findPosRE(str, re)
m = %r{re}.match(str)
return m.begin(0)
end

pos = findPosRE (“hey you behind the wall”, “/behind/”)

This gives the error:

–>undefined method ‘begin’ for nil (NameError)

How I convince ruby that the local variable m is a MatchData object, so the
begin() method can be sent to m ?

You can pass a RE directly as a paramter to the Method you are calling.
No need to pass a string and recreate a regular expression.

def findPosRE(str, re)
m = re.match(str)
m.begin(0) if m!=nil
end

pos = findPosRE (“hey you behind the wall”, /behind/)

Hope this is what you wanted,
-A.

···

Armin Roehrl, http://www.approximity.com

You shouldn’t try to convince Ruby because it is right. Your regular
expression didn’t match and nil was returned and assigned to m. Unlike
some other languages, Ruby’s regular expressions are no ugly syntax
hack. They are ordinary objects that can be passed around:

def findPosRE(str,re)
m = re.match(str) or return
m.begin(0)
end

findPosRE(“hey you behind the wall”, /behind/)
==>8

findPosRE(“hey you behind the wall”, /in front of/)
==>nil

···

Am 2002-10-22 19:56:39 +0900 schrieb T-Online:

How I convince ruby that the local variable m is a MatchData object, so the
begin() method can be sent to m ?


Faith is that quality which enables us to believe what we know to be untrue.