# Help in understanding eval function

**URL:** <https://www.ruby-forum.com/t/help-in-understanding-eval-function/174309>\
**Category:** Ruby\
**Created:** [August 24, 2009, 8:55pm UTC](https://www.ruby-forum.com/t/help-in-understanding-eval-function/174309 "2009-08-24T20:55:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ravikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@ravikanth](https://www.ruby-forum.com/u/ravikanth)\
**Post date:** [August 24, 2009, 8:55pm UTC](https://www.ruby-forum.com/t/help-in-understanding-eval-function/174309/1 "2009-08-24T20:55:32Z")

</div>

Hi  
I am new to ruby I am having trouble in understanding the eval function

can you suggest me a good reference to look at the various classes and  
methods available.

thanx in advance.

---

<div class="post-metadata">

**Author:** ![ravikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@ravikanth](https://www.ruby-forum.com/u/ravikanth)\
**Post date:** [August 24, 2009, 9:10pm UTC](https://www.ruby-forum.com/t/help-in-understanding-eval-function/174309/2 "2009-08-24T21:10:48Z")

</div>

Ravi K. wrote:

> Hi  
> I am new to ruby I am having trouble in understanding the eval function
> 
> can you suggest me a good reference to look at the various classes and  
> methods available.

Programming Ruby(2nd ed.) for ruby 1.8.x.  
Programming Ruby(3rd ed.) for ruby 1.9.x.

or…

## $ri eval ------------------------------------------------------------ Kernel#eval eval(string [, binding [, filename [,lineno]]]) =\> obj

```
 Evaluates the Ruby expression(s) in _string_. If _binding_ is
 given, the evaluation is performed in its context. The binding may
 be a +Binding+ object or a +Proc+ object. If the optional
 _filename_ and _lineno_ parameters are present, they will be used
 when reporting syntax errors.

    def getBinding(str)
      return binding
    end
    str = "hello"
    eval "str + ' Fred'" #=> "hello Fred"
    eval "str + ' Fred'", getBinding("bye") #=> "bye Fred"

```

* * *

---

<div class="post-metadata">

**Author:** ![ravikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@ravikanth](https://www.ruby-forum.com/u/ravikanth)\
**Post date:** [August 24, 2009, 9:12pm UTC](https://www.ruby-forum.com/t/help-in-understanding-eval-function/174309/3 "2009-08-24T21:12:45Z")

</div>

7stud – wrote:

> Ravi K. wrote:
> 
> > Hi  
> > I am new to ruby I am having trouble in understanding the eval function
> > 
> > can you suggest me a good reference to look at the various classes and  
> > methods available.
> 
> Programming Ruby(2nd ed.) for ruby 1.8.x.  
> Programming Ruby(3rd ed.) for ruby 1.9.x.

I guess I should explain: the second half of those books is a complete  
ruby reference, which as far as I know is the only language reference in  
print.

---

<div class="post-metadata">

**Author:** ![ravikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@ravikanth](https://www.ruby-forum.com/u/ravikanth)\
**Post date:** [August 24, 2009, 9:29pm UTC](https://www.ruby-forum.com/t/help-in-understanding-eval-function/174309/4 "2009-08-24T21:29:42Z")

</div>

On Aug 24, 2009, at 3:12 PM, 7stud – wrote:

> > Programming Ruby(2nd ed.) for ruby 1.8.x.  
> > Programming Ruby(3rd ed.) for ruby 1.9.x.
> 
> I guess I should explain: the second half of those books is a complete  
> ruby reference, which as far as I know is the only language  
> reference in  
> print.

I think that “The Ruby P.ming Language” by David Flanagan and  
Matz could be fairly considered as a ‘language reference’. Personally  
I think that David and Matz’s style is better suited for experienced  
programmers than Dave T.'s style in Programming Ruby, which I  
think is better for beginner/novice programmers.

Gary W.

---

<div class="post-metadata">

**Author:** ![ravikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@ravikanth](https://www.ruby-forum.com/u/ravikanth)\
**Post date:** [August 25, 2009, 12:20am UTC](https://www.ruby-forum.com/t/help-in-understanding-eval-function/174309/5 "2009-08-25T00:20:37Z")

</div>

On Aug 24, 11:55 am, Ravi K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi  
> I am new to ruby I am having trouble in understanding the eval function
> 
> can you suggest me a good reference to look at the various classes and  
> methods available.
> 
> ## thanx in advance.
> 
> Posted viahttp://www.ruby-forum.com/.

Dave T. provides as thorough an explanation as you could ask for  
in his Ruby Metaprogramming screencasts:

> **[Pragmatic Bookshelf: By Developers, For Developers](https://pragprog.com)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

. The fourth one in that series is dedicated to eval, but the first  
three are useful in helping you to understand it thoroughly. Also,  
David Black does a good job explaining eval (and everything else he  
addresses) in The Well-Grounded Rubyist.
