From: Armin Roehrl Date: 2002-10-22T20:18:30+09:00 Subject: Re: newby asks: giving regexps as argument for funvtions > 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