Why can't I define vars in caller's binding using eval?

Discussion in 'Ruby' started by Christopher J. Bottaro, May 22, 2008.

  1. In other words, why can't I do this?

    def f(b)
    eval("x = 10", b)
    end

    f(binding)
    puts "x = #{x}"

    Is there any way to make that code work (besides obviously setting x
    to something before calling f).

    Thanks.
    Christopher J. Bottaro, May 22, 2008
    #1
    1. Advertising

  2. Christopher J. Bottaro wrote:
    > In other words, why can't I do this?
    >
    > def f(b)
    > eval("x = 10", b)
    > end
    >
    > f(binding)
    > puts "x = #{x}"
    >
    > Is there any way to make that code work (besides obviously setting x
    > to something before calling f).
    >
    > Thanks.


    Worked for me.

    Dios:~ andrewmitchell$ irb
    >> def f(b)
    >> eval("x=10", b)
    >> end

    => nil
    >> f(binding)

    => 10
    >> puts "x = #{x}"

    x = 10
    => nil
    >>

    --
    Posted via http://www.ruby-forum.com/.
    Andrew Mitchell, May 23, 2008
    #2
    1. Advertising

  3. Christopher J. Bottaro

    Heesob Park Guest

    2008/5/23 Andrew Mitchell <>:
    > Christopher J. Bottaro wrote:
    >> In other words, why can't I do this?
    >>
    >> def f(b)
    >> eval("x = 10", b)
    >> end
    >>
    >> f(binding)
    >> puts "x = #{x}"
    >>
    >> Is there any way to make that code work (besides obviously setting x
    >> to something before calling f).
    >>
    >> Thanks.

    >
    > Worked for me.
    >
    > Dios:~ andrewmitchell$ irb
    >>> def f(b)
    >>> eval("x=10", b)
    >>> end

    > => nil
    >>> f(binding)

    > => 10
    >>> puts "x = #{x}"

    > x = 10
    > => nil
    >>>

    But it works only in irb.

    Regards,

    Park Heesob
    Heesob Park, May 23, 2008
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    As far as i know, this is because local variables have a lexical scope.

    In my experience, the parser treats everything that was not mentioned
    as a
    local variable before a method call and the runtime doesn't check
    whether
    it is a variable.

    This also explains why IRB works differently, as every statement is
    compiled
    as a single statement.

    Because of this, it is also impossible to assume the existance of
    local variables
    when executing a Proc in a certain context. (without considering hefty
    AST-Hacking
    with ruby2ruby).


    Regards,
    Florian Gilcher

    On May 23, 2008, at 11:13 AM, Heesob Park wrote:

    > 2008/5/23 Andrew Mitchell <>:
    >> Christopher J. Bottaro wrote:
    >>> In other words, why can't I do this?
    >>>
    >>> def f(b)
    >>> eval("x = 10", b)
    >>> end
    >>>
    >>> f(binding)
    >>> puts "x = #{x}"
    >>>
    >>> Is there any way to make that code work (besides obviously setting x
    >>> to something before calling f).
    >>>
    >>> Thanks.

    >>
    >> Worked for me.
    >>
    >> Dios:~ andrewmitchell$ irb
    >>>> def f(b)
    >>>> eval("x=10", b)
    >>>> end

    >> => nil
    >>>> f(binding)

    >> => 10
    >>>> puts "x = #{x}"

    >> x = 10
    >> => nil
    >>>>

    > But it works only in irb.
    >
    > Regards,
    >
    > Park Heesob
    >


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.8 (Darwin)

    iEYEARECAAYFAkg2mlsACgkQJA/zY0IIRZblSQCcD4TmWh+ebQ0bF31AZw76wfoq
    eN4Ani+na9Sg2SQaN9aGMFS73f8xKOVs
    =zFM7
    -----END PGP SIGNATURE-----
    Florian Gilcher, May 23, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    392
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  3. Linuxguy123
    Replies:
    7
    Views:
    683
    Paddy O'Loughlin
    Feb 20, 2009
  4. Mark
    Replies:
    2
    Views:
    390
  5. Stan R.
    Replies:
    1
    Views:
    132
Loading...

Share This Page