# Basics of eval?

**URL:** <https://www.ruby-forum.com/t/basics-of-eval/65590>\
**Category:** Ruby\
**Created:** [August 19, 2006, 2:40pm UTC](https://www.ruby-forum.com/t/basics-of-eval/65590 "2006-08-19T14:40:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [August 19, 2006, 2:40pm UTC](https://www.ruby-forum.com/t/basics-of-eval/65590/1 "2006-08-19T14:40:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [August 19, 2006, 4:58pm UTC](https://www.ruby-forum.com/t/basics-of-eval/65590/2 "2006-08-19T16:58:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [August 19, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/basics-of-eval/65590/3 "2006-08-19T17:16:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [August 19, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/basics-of-eval/65590/4 "2006-08-19T18:09:09Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [August 19, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/basics-of-eval/65590/5 "2006-08-19T19:07:17Z")

</div>

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/](http://onestepback.org/index.cgi/Tech/Ruby/)  
SymbolsAreNotImmutableStrings.red

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [August 19, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/basics-of-eval/65590/6 "2006-08-19T18:01:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [August 19, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/basics-of-eval/65590/7 "2006-08-19T19:11:18Z")

</div>

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
