Need help on Kernel#eval

From the link -> http://www.ruby-doc.org/core-2.0/Binding.html

Objects of class Binding encapsulate the execution context at some
particular place in the code and retain this context for future use. The
variables, ***methods, value of self, and possibly an iterator block
that can be accessed in this context are all retained***. Binding
objects can be created using Kernel#binding, and **are made available to
the callback of Kernel#set_trace_func**.

can anyone help me to understand the fact by single examples of each the
*** lines above?

···

--
Posted via http://www.ruby-forum.com/.

Why? To what purpose? What exactly are you doing? What does this apply to? What exactly are you trying to do? Show us the code where this would matter to you.

What *exactly* are you having trouble understanding? In what context did you run across this?

···

Love U Ruby <mailto:lists@ruby-forum.com>
March 26, 2013 2:33 AM
From the link -> Class: Binding (Ruby 2.0.0)

Objects of class Binding encapsulate the execution context at some
particular place in the code and retain this context for future use. The
variables, ***methods, value of self, and possibly an iterator block
that can be accessed in this context are all retained***. Binding
objects can be created using Kernel#binding, and **are made available to
the callback of Kernel#set_trace_func**.

can anyone help me to understand the fact by single examples of each the
*** lines above?

--
D. Deryl Downey

  "The bug which you would fright me with I seek" - William Shakespeare - The Winter's Tale, Act III, Scene II - A court of Justice.

Here's an example of getting the iterator block from the binding, but there is something puzzling:

def foo; binding; end
bi = foo {|x| p(x+1)}

bi.eval "yield 2" # ==> 3
eval "yield 2", binding # ==> LocalJumpError

The puzzle is: why does Binding#eval call the block, but Kernel#eval does not? Anyone...?

···

On 03/25/2013 11:33 PM, Love U Ruby wrote:

From the link -> Class: Binding (Ruby 2.0.0)
  
Objects of class Binding encapsulate the execution context at some
particular place in the code and retain this context for future use. The
variables, ***methods, value of self, and possibly an iterator block
that can be accessed in this context are all retained***. Binding
objects can be created using Kernel#binding, and **are made available to
the callback of Kernel#set_trace_func**.

D. Deryl Downey wrote in post #1103183:

Why? To what purpose? What exactly are you doing? What does this apply
to? What exactly are you trying to do? Show us the code where this would
matter to you.

What *exactly* are you having trouble understanding? In what context did
you run across this?

I have tried the below to understand the relationship between `eval` and
`Binding` object,with local variable context.

C:\>irb --simple-prompt

def display
eval("puts str",b)
^C
def display(b)
eval("puts str",b)
end

=> nil

str="hi"

=> "hi"

display(binding)

hi
=> nil

str = "hello"

=> "hello"

display(binding)

hello
=> nil

str = "good"

=> "good"

str="bad"

=> "bad"

display(binding)

bad
=> nil

The above is understood. But didn't make such simple example to
understand for **methods, value of self, and possibly an iterator block
that can be accessed in this context are all retained**. Thus asked you
like experts to give me such snippet to understand those inside the **.

···

--
Posted via http://www.ruby-forum.com/\.

Oops. That was my mistake. The last line should be

eval "yield 2", bi # ==> 3

It doesn't make a difference (as far as this example goes) whether you call Kernel#eval with a binding, or call Binding#eval.

···

On 03/26/2013 11:19 AM, Joel VanderWerf wrote:

On 03/25/2013 11:33 PM, Love U Ruby wrote:

From the link -> Class: Binding (Ruby 2.0.0)

Objects of class Binding encapsulate the execution context at some
particular place in the code and retain this context for future use. The
variables, ***methods, value of self, and possibly an iterator block
that can be accessed in this context are all retained***. Binding
objects can be created using Kernel#binding, and **are made available to
the callback of Kernel#set_trace_func**.

Here's an example of getting the iterator block from the binding, but
there is something puzzling:

def foo; binding; end
bi = foo {|x| p(x+1)}

bi.eval "yield 2" # ==> 3
eval "yield 2", binding # ==> LocalJumpError

Take the time to read through the excellent "Metaprogramming Ruby" by
Paolo Parrotta. Read it a few times. Work the examples, do the
quizzes.

···

On Tue, Mar 26, 2013 at 1:54 AM, Love U Ruby <lists@ruby-forum.com> wrote:

The above is understood. But didn't make such simple example to
understand for **methods, value of self, and possibly an iterator block
that can be accessed in this context are all retained**. Thus asked you
like experts to give me such snippet to understand those inside the **.