Forum: Ruby Values and statements

58bcce5e06cfdee6c315e5461ffe2001?d=identicon&s=25 Kalinni Gorzkis (Guest)
on 2013-10-27 03:08
(Received via mailing list)
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?
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-10-27 04:41
(Received via mailing list)
On Sat, Oct 26, 2013 at 9:08 PM, Kalinni Gorzkis
<musicdenotation@gmail.com>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?
58bcce5e06cfdee6c315e5461ffe2001?d=identicon&s=25 Kalinni Gorzkis (Guest)
on 2013-10-27 07:08
(Received via mailing list)
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 <
043da5541fa61b38d9011cfaec4836ba?d=identicon&s=25 Roy Gardiner (roy-gardiner)
on 2013-10-27 08:30
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?
09a32175057418748822c587ac08c429?d=identicon&s=25 Abinoam Jr. (abinoampraxedes_m)
on 2013-10-27 08:38
(Received via mailing list)
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-10-27 11:03
(Received via mailing list)
On Sun, Oct 27, 2013 at 3:08 AM, Kalinni Gorzkis
<musicdenotation@gmail.com> 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
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-10-27 18:50
(Received via mailing list)
On Sun, Oct 27, 2013 at 5:02 AM, Robert Klemme
<shortcutter@googlemail.com>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?
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.