newby asks: giving regexps as argument for funvtions 
Author Message
 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.



Sat, 09 Apr 2005 18:46:09 GMT  
 newby asks: giving regexps as argument for funvtions
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


Quote:
> 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.



Sat, 09 Apr 2005 19:08:26 GMT  
 newby asks: giving regexps as argument for funvtions

T> i want to pass a regular expression as a parameter to ruby. Tthe following
T> code snippet can be made easier but it shows the principle I want to
T> 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

T> def     findPosRE(str, re)
T>     m = %r{re}.match(str)
T>     return m.begin(0)
T> 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



Sat, 09 Apr 2005 19:09:59 GMT  
 newby asks: giving regexps as argument for funvtions

Quote:
> 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



Sat, 09 Apr 2005 19:18:30 GMT  
 newby asks: giving regexps as argument for funvtions
Am 2002-10-22 19:56:39 +0900 schrieb T-Online:

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

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

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



Sat, 09 Apr 2005 19:28:50 GMT  
 newby asks: giving regexps as argument for funvtions

Quote:

> ...
>  If you want to give it a String

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

        ^^^^^^^^^^^^^

I get a syntax error here, sir Guy. If I replace the first nest {} with ()
(parens), it works fine. I'm running it on irb v0.7.4

Quote:
>       m.begin(0)
>    end

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



Sat, 09 Apr 2005 19:40:02 GMT  
 newby asks: giving regexps as argument for funvtions

Quote:
>>>>> "P" == =?iso-8859-1?Q?=22Pe=F1a=2C Botp=22?= <iso-8859-1> writes:

P> I get a syntax error here, sir Guy. If I replace the first nest {} with ()
P> (parens), it works fine. I'm running it on irb v0.7.4

pigeon% irb -v
irb 0.7.4(01/05/08)
pigeon%

pigeon% irb
irb(main):001:0> str = "aaa"
"aaa"
irb(main):002:0> re = "a"
"a"
irb(main):003:0> m = %r{#{re}}.match(str)
#<MatchData:0x40195ef4>
irb(main):004:0>
pigeon%

Guy Decoux



Sat, 09 Apr 2005 19:44:32 GMT  
 newby asks: giving regexps as argument for funvtions

Quote:

> ....

> pigeon% irb -v
> irb 0.7.4(01/05/08)
> pigeon%

> pigeon% irb
> irb(main):001:0> str = "aaa"
> "aaa"
> irb(main):002:0> re = "a"
> "a"
> irb(main):003:0> m = %r{#{re}}.match(str)
> #<MatchData:0x40195ef4> irb(main):004:0>
> pigeon%

I get that too but...

irb(main):001:0> def findPosRE(str, re)
irb(main):002:1> m = %r{#{re}}.match(str)
SyntaxError: compile error
(irb):2: syntax error
        from (irb):2
              ^^^^^^^^^^^^^^^^
it errs here...

irb(main):003:0> m = %r(#{re}).match(str)
NameError: undefined local variable or method `re' for #<Object:0x2789360>
        from (irb):3

.so I had to enter again...

irb(main):004:0> def findPosRE(str, re)
irb(main):005:1> m = %r(#{re}).match(str)

note the parens....    ^     ^
and no error...

irb(main):006:1> m.begin(0)
irb(main):007:1> end
nil
irb(main):008:0>



Sat, 09 Apr 2005 20:01:28 GMT  
 newby asks: giving regexps as argument for funvtions

Quote:
>>>>> "P" == =?iso-8859-1?Q?=22Pe=F1a=2C Botp=22?= <iso-8859-1> writes:

P> irb(main):001:0> def findPosRE(str, re)
P> irb(main):002:1> m = %r{#{re}}.match(str)
P> SyntaxError: compile error
P> (irb):2: syntax error
P>         from (irb):2
P>               ^^^^^^^^^^^^^^^^

 Well, apparently this is corrected with irb 0.9 which is in 1.7.3

pigeon% irb
irb(main):001:0> def findPosRE(str, re)
irb(main):002:1> m = %r{#{re}}.match(str)
irb(main):003:1> end
nil
irb(main):004:0>
pigeon%

pigeon% irb -v
irb 0.9(02/07/03)
pigeon%

Guy Decoux



Sat, 09 Apr 2005 20:17:21 GMT  
 newby asks: giving regexps as argument for funvtions

Quote:

> ....
> P> irb(main):001:0> def findPosRE(str, re)
> P> irb(main):002:1> m = %r{#{re}}.match(str)
> P> SyntaxError: compile error
> P> (irb):2: syntax error
> P>         from (irb):2
> P>               ^^^^^^^^^^^^^^^^

>  Well, apparently this is corrected with irb 0.9 which is in 1.7.3

> pigeon% irb
> irb(main):001:0> def findPosRE(str, re)
> irb(main):002:1> m = %r{#{re}}.match(str)
> irb(main):003:1> end
> nil
> irb(main):004:0>
> pigeon%

> pigeon% irb -v
> irb 0.9(02/07/03)
> pigeon%

Many thanks for the help, sir Guy.
' will update now...

-botp



Sat, 09 Apr 2005 20:21:20 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. BUG: net/telnet.rb gives select invalid argument excepition

2. COM-VB to Python: problem with giving arguments to a function

3. TypeError: Value() takes at most 2 arguments (3 given)

4. sort(): Giving third argument?

5. python newbie error: TypeError: get() takes exactly 1 argument (2 given)

6. how to give an object as argument for a method

7. Giving wish a start-up argument

8. You give me TLINK+TASM and I give you a VIRUS CREATION SET

9. do SED supports extended regexps?

10. Regexps in PARSE

11. Regexps and anchoring again

12. RegExps and latin_1

 

 
Powered by phpBB® Forum Software