A quetion about ruby string

Z

Zhao Yi

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.
 
V

Vladimir Fekete

Hi,

classical #{} works...

e.g:

a = "asd"

puts "SAME" if "asd" =~ /#{a}/

Cheers,

V.
 
P

Peña, Botp

RnJvbTogWmhhbyBZaSBbbWFpbHRvOnlvdWhhb2RleWlAZ21haWwuY29tXSANCiMgSG93IGNhbiBJ
IGNoYW5nZSBhIHN0cmluZyB0byByZWd1bGFyIGV4cHJlc3Npb24/IFRha2UgdGhpcyANCiMgbWV0
aG9kIGZvciBhbg0KIyBleGFtcGxlOg0KDQo+cXJpIFJlZ2V4cCNuZXcNCi0tLS0tLS0tLS0tDQog
UmVnZXhwLm5ldyhzdHJpbmcgWywgb3B0aW9ucyBbLCBsYW5nXV0pICAgICAgID0+IHJlZ2V4cA0K
ICAgICBSZWdleHAubmV3KHJlZ2V4cCkgICAgICAgICAgICAgICAgICAgICAgICAgICAgPT4gcmVn
ZXhwDQogICAgIFJlZ2V4cC5jb21waWxlKHN0cmluZyBbLCBvcHRpb25zIFssIGxhbmddXSkgICA9
PiByZWdleHANCiAgICAgUmVnZXhwLmNvbXBpbGUocmVnZXhwKSAgICAgICAgICAgICAgICAgICAg
ICAgID0+IHJlZ2V4cA0KLS0tLS0tLS0tLS0NCkNvbnN0cnVjdHMgYSBuZXcgcmVndWxhciBleHBy
ZXNzaW9uIGZyb20gcGF0dGVybiwgd2hpY2ggY2FuIGJlDQplaXRoZXIgYSBTdHJpbmcgb3IgYSBS
ZWdleHAgKGluIHdoaWNoIGNhc2UgdGhhdCByZWdleHAncyBvcHRpb25zDQphcmUgcHJvcGFnYXRl
ZCwgYW5kIG5ldyBvcHRpb25zIG1heSBub3QgYmUgc3BlY2lmaWVkIChhIGNoYW5nZSBhcw0Kb2Yg
UnVieSAxLjgpLiBJZiBvcHRpb25zIGlzIGEgRml4bnVtLCBpdCBzaG91bGQgYmUgb25lIG9yIG1v
cmUgb2YNCnRoZSBjb25zdGFudHMgUmVnZXhwOjpFWFRFTkRFRCwgUmVnZXhwOjpJR05PUkVDQVNF
LCBhbmQgUmVnZXhwOjpNVUxUSUxJTkUsIG9yLWVkIHRvZ2V0aGVyLi4uLg0K
 
F

F. Senault

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
 
Z

Zhao Yi

F. Senault said:
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.
 
S

Siep Korteling

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(r)

s = "ello"
match(s)

hth,

Siep
 
V

Vladimir Fekete

Hi,

F. Senault said:
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.

imho, you can retype it to string by .to_s


cheers,

V.

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top