Values and statements

Ruby has a peculiar behavior:

  1. eval(statement) will create it’s own scope and run the commands in
    that scope, while eval statement will run the commands in the global
    scope.
  2. I can assign something to, for example, puts and the puts
    statement will work just like without the assignment.

How does this work?

On Sat, Oct 26, 2013 at 9:08 PM, Kalinni Gorzkis
[email protected]wrote:

Ruby has a peculiar behavior:

  1. eval(statement) will create it’s own scope and run the commands in
    that scope, while eval statement will run the commands in the global scope.
  2. I can assign something to, for example, puts and the puts
    statement will work just like without the assignment.

How does this work?

Could you please show real code that exhibits the behaviour you’re
wondering about?

puts=3
puts “Hello World!”
puts puts
eval(“x=3”)
puts x
eval “x=3”
puts x

On Sun, Oct 27, 2013 at 10:41 AM, tamouse mailing lists <

I get

Hello World!
3
3
3

which is what I would expect?

Newbie warning — should ‘eval(x=3)’ and ‘eval x=3’ work differently?

What versions are you testing it on?

In my setup no difference between eval “x=3” and eval(“x=3”).

Try this “before” the eval.

x = “Now the local variable exists before the eval”
eval(“x=3”)
puts x
eval “x=3”
puts x

IMHO eval string is evaluated as a closure like a block and the rules
for local variable scopes (at blocks) apply.

Look at this:

10.times {|n| x = n; puts x }; puts x # => Render error because ‘x’
was restricted to the block.

x = “Anything before entering the block”; 10.times {|n| x = n; puts x
}; puts x # => Now, no error.

If it was an instance variable it would render no error because
instance variables rules are different.

10.times {|n| @x = n; puts @x }; puts @x # => No error, even if
instance var ‘@x’ is not previously defined.

NOTE: Ruby 1.8 rules are different. It will probably issue you no
error in any of your examples.

Abinoam Jr.

On Sun, Oct 27, 2013 at 3:07 AM, Kalinni Gorzkis

On Sun, Oct 27, 2013 at 5:02 AM, Robert K.
[email protected]wrote:

on the second optional argument which you can use to pass another
I chime in with others who have asked you to provide real code
examples that demonstrate the behavior. Please do.

Cheers

robert


remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

pry(main)> puts = “some silly string”
=> “some silly string”

pry(main)> puts puts
some silly string
=> nil

pry(main)> just_a_var = “I am just a variable”
=> “I am just a variable”

pry(main)> puts just_a_var
I am just a variable
=> nil

pry(main)> puts
=> “some silly string”

Difference between method/messages/local vars here is all, yes?

On Sun, Oct 27, 2013 at 3:08 AM, Kalinni Gorzkis
[email protected] wrote:

Ruby has a peculiar behavior:

eval(statement) will create it’s own scope and run the commands in that
scope, while eval statement will run the commands in the global scope.

No. There’s no difference between calling eval with or without
brackets other than syntax issues because of precedence. But eval
behaves identical in both cases. If at all behavior can differ based
on the second optional argument which you can use to pass another
binding.

I can assign something to, for example, puts and the puts statement will
work just like without the assignment.

Dark is the meaning of your words.

How does this work?

I chime in with others who have asked you to provide real code
examples that demonstrate the behavior. Please do.

Cheers

robert