Forum: Ruby A quetion about ruby string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Zhao Y. (Guest)
on 2008-12-08 11:11
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.
Vladimir F. (Guest)
on 2008-12-08 11:26
(Received via mailing list)
Hi,

  classical #{} works...

e.g:

 a = "asd"

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

Cheers,

 V.
Peña, Botp (Guest)
on 2008-12-08 11:28
(Received via mailing list)
From: Zhao Yi [mailto:removed_email_address@domain.invalid]
# 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 (Guest)
on 2008-12-08 11:55
(Received via mailing list)
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
Zhao Y. (Guest)
on 2008-12-08 14:07
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.
Siep K. (Guest)
on 2008-12-08 14:36
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
Vladimir F. (Guest)
on 2008-12-08 15:18
(Received via mailing list)
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.
This topic is locked and can not be replied to.