A quetion about ruby string

How can I change a string to regular expression? Take this method for an
example:

def match(pattern)

end

This method accepts one parameter as a regular expression pattern, when
I invoke this method by

match(/…/)

it works fine. But if I invoke this method by

param="/…/"
match(param)

it doesn’t work as expected. It seems that the string is different with
the regular expression. If the pattern stored in a string variable, how
can I use it?

thanks.

Hi,

classical #{} works…

e.g:

a = “asd”

puts “SAME” if “asd” =~ /#{a}/

Cheers,

V.

From: Zhao Yi [mailto:[email protected]]

How can I change a string to regular expression? Take this

method for an

example:

qri Regexp#new


Regexp.new(string [, options [, lang]]) => regexp
Regexp.new(regexp) => regexp
Regexp.compile(string [, options [, lang]]) => regexp
Regexp.compile(regexp) => regexp

Constructs a new regular expression from pattern, which can be
either a String or a Regexp (in which case that regexp’s options
are propagated, and new options may not be specified (a change as
of Ruby 1.8). If options is a Fixnum, it should be one or more of
the constants Regexp::EXTENDED, Regexp::IGNORECASE, and
Regexp::MULTILINE, or-ed together…

F. Senault wrote:

And, on top of what the others have said, you don’t need the forward
slashes - they’re Ruby (and Perl, and…) constructs, not part of the
regexp. Otherwise :

a = “/abc[def]/”
=> “/abc[def]/”

Regexp.new(a)
=> //abc[def]//

b = “abc[def]”
=> “abc[def]”

Regexp.new(b)
=> /abc[def]/

(Be careful if you have Wirble installed ; mine completely suppresses
all the /, which kinda defeats the purpose of the exercise…)

Fred

Does Ruby support RTTI (runtime type identification)? This problem is
that the type of the parameter should be Regexp instance, however ruby
doesn’t check type on the method declaration. So I am looking for this
kind of method:

def match(pattern)
if pattern instanceof String
p = Regexp.new(pattern)
end

end

In this way, I can check the type of the parameter so that the client
code doesn’t need to change.

Le 8 décembre 2008 à 10:04, Zhao Yi a écrit :

param="/…/"
match(param)

it doesn’t work as expected. It seems that the string is different with
the regular expression. If the pattern stored in a string variable, how
can I use it?

And, on top of what the others have said, you don’t need the forward
slashes - they’re Ruby (and Perl, and…) constructs, not part of the
regexp. Otherwise :

a = “/abc[def]/”
=> “/abc[def]/”

Regexp.new(a)
=> //abc[def]//

b = “abc[def]”
=> “abc[def]”

Regexp.new(b)
=> /abc[def]/

(Be careful if you have Wirble installed ; mine completely suppresses
all the /, which kinda defeats the purpose of the exercise…)

Fred

Hi,

On Mon, Dec 08, 2008 at 09:01:20PM +0900, Zhao Yi wrote:

=> “abc[def]”
doesn’t check type on the method declaration. So I am looking for this
code doesn’t need to change.
imho, you can retype it to string by .to_s

cheers,

V.

Zhao Yi wrote:
(…)

Does Ruby support RTTI (runtime type identification)? This problem is
that the type of the parameter should be Regexp instance, however ruby
doesn’t check type on the method declaration. So I am looking for this
kind of method:

def match(pattern)
if pattern instanceof String
p = Regexp.new(pattern)
end

end

In this way, I can check the type of the parameter so that the client
code doesn’t need to change.

pattern.instance_of? String

But why check if it is a string or a regexp, when Regexp.new accepts
both?

def match(pattern)
puts “Hello” =~ Regexp.new(pattern)
end

r = /ello/
puts r.instance_of?(Regexp)
match®

s = “ello”
match(s)

hth,

Siep