Basics of eval?

Hi all,

I’m a ‘ruby nubie’ and otherwise unfamiliar with the ‘:eval’ technique
it employs.

Are there any decent reference guides to it’s use?

Alternatively, can anyone give me a quick explanation of what, when and
how to use it?

This for example, from an ajax scaffold im working on, makes little
sense to me (it doesn’t help that it’s also Saturday afternoonr!)

:eval => “detail.bookings.collect{ |booking| booking.date }.join(’, ')”

Many thanks,

Jon.

On Aug 19, 2006, at 13:40, Jon wrote:

This for example, from an ajax scaffold im working on, makes little
sense to me (it doesn’t help that it’s also Saturday afternoonr!)

:eval => “detail.bookings.collect{ |booking| booking.date }.join(’,
')”

There’s actually several different things going on, so I’ll give my
best explanation one by one.

First, names prefixed with a colon, like :eval, are symbols. If
you’re familiar with Lisp, then that’s good, because they’re quite
similar to Lisp symbols. If you’re not familiar with symbols in
Lisp, then a quick search of the mailing list archives should provide
plenty of explanations for symbols. For now, though, you can
consider them as a literal value, a little like an integer (e.g., 12)
but without all the arithmetic, basically, they have a unique value
in and of themselves, and can be compared (:foo == :foo, :foo != :bar).

Next, you’ve got a hash literal - hash literals don’t always need the
enclosing {}. The symbol is being used as a key in the hash, which
is a very common use of symbols. For example:

p :foo => “bar”, :moose => “squirrel”
{:foo=>“bar”, :moose=>“squirrel”}

So what you have is a hash whose key is the symbol :eval, and whose
value is a string of Ruby code that looks a lot like something to do
with ActiveRecord. Perhaps the key to the puzzle is that there’s an
eval method in Ruby which takes a string and interprets it as Ruby
code, for example:

eval(‘p :foo => “bar”, :moose => “squirrel”’)

Will get the same result as the example above.

What I suspect is going on is that the hash is being used to (perhaps
optionally) provide a snippet of Ruby code to be executed later in
the program.

Symbols and hash literals creep up all over the place in Ruby code,
so there’s no particular advice I could give about when it’s
appropriate to use them. Eval is somewhat trickier - it and its
cousins instance_eval and class_eval which are used extensively in
metaprogramming in Ruby, and it’s obviously handy to be able to pass
code snippets around as strings in some tasks. They do have their
pitfalls, though. Again, a search of the mailing list should give
interesting discussions about when where and how to use eval.

Hope that’s of some help.

matthew smillie.

Jon wrote:

This for example, from an ajax scaffold im working on, makes little
sense to me (it doesn’t help that it’s also Saturday afternoonr!)

:eval => “detail.bookings.collect{ |booking| booking.date }.join(’, ')”

Many thanks,

Jon.

You use eval with ajaxscaffold when you want to supply more complicated
formula to display a cell value.
In simpler case you just use field name e.g.

AjaxScaffold::ScaffoldColumn.new(self, { :name => “some_field”,:label =>
“some_label”.r})

Then ajaxscaffold can use just send method to obtain field value.

When you use associations then case is more complicated:

AjaxScaffold::ScaffoldColumn.new(self, { :name => “some_field_id”,:label
=> “some_label”.r, :eval => “some_model.some_field.some_property” ,
:filter => ‘some_filter’})

and ajax scaffold must use eval to obtain a value (it is slower, but in
ruby 1.8.5 seems to be quite fast)

the “detail.bookings.collect{ |booking| booking.date }.join(’, ')” is
quite complex so eval must be used

look for <% column_value = eval(scaffold_column.eval) rescue %>
in your generated ajaxscaffold code.

In simpler cases like “some_model.some_field.some_property” you could do
a send trick:

def send_eval s
s.split(’.’).inject(self){|a,b| a.send(b.to_sym)}
end

but it seems that ruby’s eval is a lot faster:

class A
attr_accessor :a

 def x
     "foo"
 end

end

class B
attr_accessor :a

 def initialize
     @a=A.new
 end

end

class C
attr_accessor :b

 def initialize
     @b=B.new
 end

end

$c=C.new

def c
$c
end

def send_eval s
s.split(’.’).inject(self){|a,b| a.send(b.to_sym)}
end

s=“c.b.a.x”

n = 10000
require ‘benchmark’
Benchmark.bm do |x|
x.report(“eval”) { n.times{ eval s } }
x.report(“send_eval”) { n.times{ send_eval s } }
end

here are the results:

ruby 1.8.4:

   user     system      total        real

eval 0.219000 0.000000 0.219000 ( 0.219000)
send_eval 0.343000 0.000000 0.343000 ( 0.359000)

ruby 1.8.5 preview4

   user     system      total        real

eval 0.109000 0.000000 0.109000 ( 0.117000)
send_eval 0.375000 0.000000 0.375000 ( 0.374000)

lopex

Jon wrote:

Thanks guys, some outstandingly useful and pertinent advice.

I know it’s a common complaint with Ruby, but is there no online or
offline documentation regarding eval?
Check http://ruby-doc.org/core/classes/Kernel.src/M003216.html. No idea
what context your string’s getting evaluated in, though… That’d be
ajaxscaffold-specific.

On Aug 19, 2006, at 9:57 AM, Matthew S. wrote:

First, names prefixed with a colon, like :eval, are symbols. If
you’re familiar with Lisp, then that’s good, because they’re quite
similar to Lisp symbols. If you’re not familiar with symbols in
Lisp, then a quick search of the mailing list archives should
provide plenty of explanations for symbols.

This is a very good description of symbols:

http://onestepback.org/index.cgi/Tech/Ruby/
SymbolsAreNotImmutableStrings.red

James Edward G. II

Thanks guys, some outstandingly useful and pertinent advice.

I know it’s a common complaint with Ruby, but is there no online or
offline documentation regarding eval?

Cheers again,

Jon.

On Aug 19, 2006, at 11:00 AM, Jon wrote:

I know it’s a common complaint with Ruby, but is there no online or
offline documentation regarding eval?

The book Ruby for Rails has a section that explains and compares the
various forms of evaluating Ruby source code (eval(), instance_eval
(), class_eval()/module_eval()). It’s very well done.

James Edward G. II