Limitations of eval

A

Adelle Hartley

Hi all,

I have been using eval to execute code fragments that I fetch from a
database.

Imagine my surprise when I discovered that the following does not work when
executed by eval:

unless thing.nil?
puts "I have something"
else
puts "nothing here"
end

This works as expected when executed "properly" (from within a regular
normal .rb file).

I don't seem able to make any conditional code work correctly with eval,
unless the conditinoal branch is a one liner:

puts "I have something" unless thing.nil?

Are the limitations of eval documented somewhere (or is it possible that I'm
just not doing it right)?

Adelle.
 
D

David A. Black

Hi --

Hi all,

I have been using eval to execute code fragments that I fetch from a
database.

(shudder :) But I'm sure you've heard all of that already :)
Imagine my surprise when I discovered that the following does not work when
executed by eval:

unless thing.nil?
puts "I have something"
else
puts "nothing here"
end

This works as expected when executed "properly" (from within a regular
normal .rb file).

I don't seem able to make any conditional code work correctly with eval,
unless the conditinoal branch is a one liner:

puts "I have something" unless thing.nil?

Are the limitations of eval documented somewhere (or is it possible that I'm
just not doing it right)?

I can't duplicate the problem:

$ ruby
thing = 1
eval '
unless thing.nil?
puts "I have something"
else
puts "nothing here"
end'
^D
I have something


David
 
R

Robert Klemme

David A. Black said:
Hi --



(shudder :) But I'm sure you've heard all of that already :)

*eval grin*
I can't duplicate the problem:

$ ruby
thing = 1
eval '
unless thing.nil?
puts "I have something"
else
puts "nothing here"
end'
^D
I have something

My guess is that the string probably contained something different than
Adelle expected. Adelly, did you try to output your string with "p" before
you eval it? Might be interesting to see what's really in there. As an
illustration what I mean:
(eval):1: warning: parenthesize argument(s) for future version
SyntaxError: (eval):1:in `irb_binding': compile error
(eval):1: syntax error
unless thing puts 'y' else puts 'n' end
^
(eval):1: syntax error
unless thing puts 'y' else puts 'n' end
^
from (irb):9
from (irb):9n
=> nil

I.e. you probably do not have the line breaks in your string and since you
don't use "then" syntax is not correct. It doesn't work in a normal line
without "then" also:
(irb):11: warning: parenthesize argument(s) for future version
SyntaxError: compile error
(irb):11: syntax error
unless thing puts 'y' else puts 'n' end
^
(irb):11: syntax error
from (irb):11

Kind regards

robert
 
A

Adelle Hartley

David said:
(shudder :) But I'm sure you've heard all of that already :)

Don't worry, it's a highly experimental prototype of something that I don't
intend to release.
I can't duplicate the problem:

I don't know what I was doing wrong, since it looked correct when I output
the code using puts. Anyway, it's working now.

Thanks.

Adelle.
 

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,596
Members
45,143
Latest member
DewittMill
Top