Using eval() to create a Regexp object from a string

I

Ivan V.

Hi,

I'm trying to create a Regexp object by calling eval like this:

def eval_to_regexp(o)
if o.is_a?(Regexp)
o
elsif o.is_a?(String)
o = o[5..o.size] if o[0..4] == 'eval:'
eval("/#{o}/")
else
eval("/#{o.to_s}/")
end
end

e = eval_to_regexp("eval:some_function() + '^'")

Suppose some_function() returns the string '/home/test'

And then I use it to test a match as follows:

'/home/test' =~ e # should return 0
'/home/test/somethingelse' =~ e # should return nil (because of the '^'
above)

I also tried using /#{o}/ directly instead of the eval, but it doesn't
work either :(

Thanks in advance!

- Ivan V.
 
S

Sebastian Hungerecker

Ivan said:
I'm trying to create a Regexp object by calling eval like this:

I'm sorry, but why don't you just do regexp=Regexp.new(your_string) ?
 
D

dblack

Hi --

Hi,

I'm trying to create a Regexp object by calling eval like this:

def eval_to_regexp(o)
if o.is_a?(Regexp)
o
elsif o.is_a?(String)
o = o[5..o.size] if o[0..4] == 'eval:'
eval("/#{o}/")
else
eval("/#{o.to_s}/")
end
end

e = eval_to_regexp("eval:some_function() + '^'")

Suppose some_function() returns the string '/home/test'

And then I use it to test a match as follows:

'/home/test' =~ e # should return 0
'/home/test/somethingelse' =~ e # should return nil (because of the '^'
above)

I also tried using /#{o}/ directly instead of the eval, but it doesn't
work either :(

I think you might have better luck with Regexp.new:

e = Regexp.new("^#{some_function}")

or something like that.


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
I

Ivan V.

Sebastian said:
I'm sorry, but why don't you just do regexp=Regexp.new(your_string) ?

Because I didn't thought about that and I'm a complete newb regarding
Ruby and Regexp (in general), as pointed out by David's correction (the
'^' is for the start of the string, not the end - thanks!).

Thanks a lot :)

- Ivan V.
 
J

James Edward Gray II

Suppose some_function() returns the string '/home/test'

I see you already have your answer, but another thing you may want to
be aware of is Regexp.escape():

def some_function; '/home/test' end

re = /#{Regexp.escape(some_function)}/ # => /\/home\/test/
re.source # => "/home/test"

James Edward Gray II
 
G

Giles Bowkett

I'm trying to create a Regexp object by calling eval like this:
Because I didn't thought about that and I'm a complete newb regarding
Ruby and Regexp (in general), as pointed out by David's correction (the
'^' is for the start of the string, not the end - thanks!).

Be careful about eval()! I definitely enjoy eval() but you need to
have some perspective. It's not for newbs.
 

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,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top