instance_eval

Discussion in 'Ruby' started by lopex, Aug 17, 2004.

  1. lopex

    lopex Guest

    Hi,

    I'm wondering why the following code works:

    class X
    end

    x=X.new
    a="some variable"


    p x.instance_eval("a")
    x.instance_eval{p a}

    why the x object knows the a variable ?

    TIA


    Marcin Mielzynski
     
    lopex, Aug 17, 2004
    #1
    1. Advertising

  2. lopex

    lopex Guest

    lopex wrote:

    Ok. the following works since a gets captured by clousure

    > x.instance_eval{p a}


    but

    x.instance_eval("a")

    is a plain function call

    > Marcin Mielzynski
     
    lopex, Aug 17, 2004
    #2
    1. Advertising

  3. Hi --

    On Tue, 17 Aug 2004, lopex wrote:

    > Hi,
    >
    > I'm wondering why the following code works:
    >
    > class X
    > end
    >
    > x=X.new
    > a="some variable"
    >
    >
    > p x.instance_eval("a")
    > x.instance_eval{p a}
    >
    > why the x object knows the a variable ?


    instance_eval sets 'self' to the receiver (x in this case), but it's
    still happening in the existing scope, where 'a' is defined.


    David

    --
    David A. Black
     
    David A. Black, Aug 17, 2004
    #3
  4. lopex

    ts Guest

    >>>>> "l" == lopex <> writes:

    l> x.instance_eval("a")

    l> is a plain function call

    What do you expect with ?

    a = "some variable"
    p eval("a")


    Guy Decoux
     
    ts, Aug 17, 2004
    #4
  5. lopex

    lopex Guest

    ts wrote:


    > What do you expect with ?
    >
    > a = "some variable"
    > p eval("a")


    I read in rdoc that instance_eval changes eval context to the receiver,
    so a thought the scope changes as well.

    Marcin Mielzynski
     
    lopex, Aug 17, 2004
    #5
  6. lopex

    ts Guest

    >>>>> "l" == lopex <> writes:

    l> I read in rdoc that instance_eval changes eval context to the receiver,
    l> so a thought the scope changes as well.

    It change self and the default class

    svg% cat b.rb
    #!/usr/bin/ruby
    class A
    end

    a = A.new
    b = A.new

    a.instance_eval("def a() puts 'a' end")
    a.a
    b.a
    svg%

    svg% b.rb
    a
    /b.rb:10: undefined method `a' for #<A:0x4009a024> (NoMethodError)
    svg%


    Guy Decoux
     
    ts, Aug 17, 2004
    #6
  7. lopex

    lopex Guest

    ts wrote:

    >
    > It change self and the default class


    So it seems it uses the class accessor of self
    (btw. the def construct works in this way ? haven't seen it is a method
    like e.g. private, protected etc.)

    so why the given example outputs nil: ?

    ---
    class A
    def initialize
    # @v="value"
    end
    end

    a=A.new

    @v="second value"

    p a.instance_eval("@v")
    ---

    I know that there is no instance object @v in 'a', but if the scope
    remains the same, should we have an access to @v defined in outer object
    of class Object ?
    Or is the the reason we always can use instance variable identifier even
    it was not defined sa far... ?

    TIA

    Marcin Mielzynski
     
    lopex, Aug 17, 2004
    #7
  8. lopex

    ts Guest

    >>>>> "l" == lopex <> writes:

    l> so why the given example outputs nil: ?

    An instance variable belong to an object, not to a scope.


    Guy Decoux
     
    ts, Aug 17, 2004
    #8
  9. lopex

    lopex Guest

    ts wrote:


    > An instance variable belong to an object, not to a scope.


    Thanks. It's clear now for me.

    Marcin Mielzynski
     
    lopex, Aug 17, 2004
    #9
  10. Weird PTY behaviour...

    I have some weird problems...

    ...am trying to run pico via pty. It works, except that if you run it
    though pty, it wants to justify all your text.

    tried nano, but it's control codes don't work right, but the justification
    problems don't happen. Very odd.

    Anyone know of a simple terminal mode editor for linux that will work via
    pty? thanks...

    Mark

    "But Schindler is bueno! Senior Burns is El Diablo!"

    --------------------------------------------------------------
    Website - http://www.retrobbs.org
    Tradewars - telnet tradewars.retrobbs.org
    BBS - http://bbs.retrobbs.org:8000
    IRC - irc.retrobbs.org #main
    WIKI - http://www.tpoh.org/cgi-bin/tpoh-wiki

    ----- Original Message -----
    From: "ts" <>
    To: "ruby-talk ML" <>
    Cc: <>
    Sent: Tuesday, August 17, 2004 4:04 PM
    Subject: Re: instance_eval


    > >>>>> "l" == lopex <> writes:

    >
    > l> so why the given example outputs nil: ?
    >
    > An instance variable belong to an object, not to a scope.
    >
    >
    > Guy Decoux
    >
    >
     
    Mark Firestone, Aug 17, 2004
    #10
  11. lopex

    Richard Dale Guest

    lopex wrote:

    > Hi,
    >
    > I'm wondering why the following code works:
    >
    > class X
    > end
    >
    > x=X.new
    > a="some variable"
    >
    >
    > p x.instance_eval("a")
    > x.instance_eval{p a}
    >
    > why the x object knows the a variable ?

    Maybe this makes it clearer:

    class X
    end

    x=X.new
    a="some variable"

    def m1(thing, &block)
    block.call(thing)
    end

    def m2(thing)
    thing.instance_eval{p a}
    end

    p x.instance_eval("a")
    x.instance_eval{p a}
    m1(x) {p a}
    m2(x)

    The m1() method works because it is passed a block where 'a' is in scope.
    Then in m1 the block is called with that complete context, ie including
    'a'.

    Whereas the m2() method fails because instance_eval() runs within the
    context of just 'thing', ie 'x', without its environment.

    "some variable"
    "some variable"
    "some variable"
    try_instance_eval.rb:12:in `m2': undefined local variable or method `a' for
    #<X:0x3004c0d8> (NameError)

    -- Richard
     
    Richard Dale, Aug 17, 2004
    #11
    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. Replies:
    1
    Views:
    106
    Mauricio Fernández
    Jul 17, 2003
  2. Tim Bates

    instance_eval and Class Variables

    Tim Bates, Aug 6, 2003, in forum: Ruby
    Replies:
    0
    Views:
    123
    Tim Bates
    Aug 6, 2003
  3. Aron Griffis

    instance_eval and constants

    Aron Griffis, Feb 24, 2004, in forum: Ruby
    Replies:
    2
    Views:
    99
    Aron Griffis
    Feb 24, 2004
  4. Joel VanderWerf

    instance_eval from C ext

    Joel VanderWerf, Apr 26, 2004, in forum: Ruby
    Replies:
    3
    Views:
    206
    Joel VanderWerf
    May 3, 2004
  5. Francis Hwang
    Replies:
    2
    Views:
    137
    Francis Hwang
    Oct 7, 2004
Loading...

Share This Page