Inconsistent block parameters handling by instance_eval

Discussion in 'Ruby' started by Patrick Li, Aug 8, 2008.

  1. Patrick Li

    Patrick Li Guest

    class A
    def A.method &block
    A.new.instance_eval &block
    end

    def A.method2 &block
    A.new.instance_eval {block.call}
    end
    end

    A.method{p self} #prints #<A:0x28c2858>

    A.method2{p self} #prints main

    From this example: is instance_eval even working in method2? It clearly
    shows that block is not called in A's context.
    -Patrick
    --
    Posted via http://www.ruby-forum.com/.
     
    Patrick Li, Aug 8, 2008
    #1
    1. Advertisements

  2. On Friday 08 August 2008, Patrick Li wrote:
    > class A
    > def A.method &block
    > A.new.instance_eval &block
    > end
    >
    > def A.method2 &block
    > A.new.instance_eval {block.call}
    > end
    > end
    >
    > A.method{p self} #prints #<A:0x28c2858>
    >
    > A.method2{p self} #prints main
    >
    > From this example: is instance_eval even working in method2? It clearly
    > shows that block is not called in A's context.
    > -Patrick


    The effect of instance_eval is to set the variable "self" to the receiver for
    the duration of the block. In the first case, everything works as expected. In
    the second case, instead, self is changed only for the outer block, while in
    the inner block "self" remains what it were when the block was defined.
    There's nothing new here: the environment in which the block is executed
    doesn't affect its variables and the fact that instance_eval treats the outer
    block in a special way doesn't otherwise change things.

    I hope this helps

    Stefano
     
    Stefano Crocco, Aug 8, 2008
    #2
    1. Advertisements

  3. Patrick Li

    Patrick Li Guest

    hmmm.... that makes sense.

    It seems a bit counter-intuitive though. .. but i guess it would have to
    be this way, otherwise closures won't work.
    Thanks for the explanation Stefano.
    -Patrick
    --
    Posted via http://www.ruby-forum.com/.
     
    Patrick Li, Aug 8, 2008
    #3
    1. Advertisements

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. morrell
    Replies:
    1
    Views:
    1,234
    roy axenov
    Oct 10, 2006
  2. Francis Hwang
    Replies:
    2
    Views:
    192
    Francis Hwang
    Oct 7, 2004
  3. Adam Anderson
    Replies:
    6
    Views:
    297
    Gary Wright
    Nov 7, 2007
  4. Vincent L.
    Replies:
    4
    Views:
    258
    Charles Oliver Nutter
    Jun 4, 2008
  5. Nit Khair
    Replies:
    1
    Views:
    157
    Robert Klemme
    Oct 22, 2008
  6. LAMBEAU Bernard
    Replies:
    9
    Views:
    248
    Mike Gold
    Jan 8, 2009
  7. Robert Klemme
    Replies:
    4
    Views:
    177
    Robert Klemme
    Oct 8, 2009
  8. Giacomo Alzetta
    Replies:
    10
    Views:
    401
    Giacomo Alzetta
    Nov 22, 2012
Loading...