# A quetion about ruby string

**URL:** <https://www.ruby-forum.com/t/a-quetion-about-ruby-string/155114>\
**Category:** Ruby\
**Created:** [December 8, 2008, 10:11am UTC](https://www.ruby-forum.com/t/a-quetion-about-ruby-string/155114 "2008-12-08T10:11:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 8, 2008, 10:11am UTC](https://www.ruby-forum.com/t/a-quetion-about-ruby-string/155114/1 "2008-12-08T10:11:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 8, 2008, 10:26am UTC](https://www.ruby-forum.com/t/a-quetion-about-ruby-string/155114/2 "2008-12-08T10:26:58Z")

</div>

Hi,

classical #{} works…

e.g:

a = “asd”

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

Cheers,

V.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 8, 2008, 10:28am UTC](https://www.ruby-forum.com/t/a-quetion-about-ruby-string/155114/3 "2008-12-08T10:28:45Z")

</div>

From: Zhao Yi [[mailto:removed\_email\_address@domain.invalid](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…

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 8, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/a-quetion-about-ruby-string/155114/4 "2008-12-08T13:07:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 8, 2008, 10:55am UTC](https://www.ruby-forum.com/t/a-quetion-about-ruby-string/155114/5 "2008-12-08T10:55:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 8, 2008, 2:18pm UTC](https://www.ruby-forum.com/t/a-quetion-about-ruby-string/155114/6 "2008-12-08T14:18:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 8, 2008, 1:36pm UTC](https://www.ruby-forum.com/t/a-quetion-about-ruby-string/155114/7 "2008-12-08T13:36:43Z")

</div>

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
