Calling a block with parameters in a new scope

Discussion in 'Ruby' started by Ruby Talk, May 20, 2007.

  1. Ruby Talk

    Ruby Talk Guest

    Calling a block in a new scope is easy enough:

    self.instance_eval &block

    And calling a block, passing it parameters is easy enough:

    block.call(*params)

    But is there some way to do both? To both change the scope of evaluation
    and pass variables into the block.
    Ruby Talk, May 20, 2007
    #1
    1. Advertising

  2. Ruby Talk

    Matt Gretton Guest

    Ruby Talk wrote:
    > Calling a block in a new scope is easy enough:
    >
    > self.instance_eval &block
    >
    > And calling a block, passing it parameters is easy enough:
    >
    > block.call(*params)
    >
    > But is there some way to do both? To both change the scope of evaluation
    > and pass variables into the block.


    Hello,

    I'm not sure I fully understand the question being asked here. Can you
    provide a small bit of code to highlight what you are trying to acheive.

    Do you wish to pass vaiables into the instance_eval block?

    Any variables in scope when the instance_eval block is called can be
    used in the instance_eval block itself. However, if you were to pass a
    proc object into the instance_eval it would retain the context it was
    created at.

    See below for an example of this.

    class Test
    def initialize(arg)
    @instance_variable = arg
    end
    end

    test_obj = Test.new("initializing_arg")

    outside = "outside instance_eval"

    proc = Proc.new {|arg| puts self} #self main here

    test_obj.instance_eval do
    puts @instance_variable #test_obj instance variable.
    puts outside #any variable from outside the block can be passed in.
    puts proc #will return main rather than the object
    end

    Hmm, I get the feeling I have ranted about stuff that does not help you
    one bit with your question...

    Get back to me with a bit more detail and hopefully I'll be able to
    help!

    Cheers,

    Matt.



    --
    Posted via http://www.ruby-forum.com/.
    Matt Gretton, May 20, 2007
    #2
    1. Advertising

  3. Ruby Talk

    Matt Gretton Guest

    Matt Gretton wrote:
    > Ruby Talk wrote:
    >> Calling a block in a new scope is easy enough:
    >>
    >> self.instance_eval &block
    >>
    >> And calling a block, passing it parameters is easy enough:
    >>
    >> block.call(*params)
    >>
    >> But is there some way to do both? To both change the scope of evaluation
    >> and pass variables into the block.

    >
    > Hello,
    >
    > I'm not sure I fully understand the question being asked here. Can you
    > provide a small bit of code to highlight what you are trying to acheive.
    >
    > Do you wish to pass vaiables into the instance_eval block?
    >
    > Any variables in scope when the instance_eval block is called can be
    > used in the instance_eval block itself. However, if you were to pass a
    > proc object into the instance_eval it would retain the context it was
    > created at.
    >
    > See below for an example of this.
    >
    > class Test
    > def initialize(arg)
    > @instance_variable = arg
    > end
    > end
    >
    > test_obj = Test.new("initializing_arg")
    >
    > outside = "outside instance_eval"
    >
    > proc = Proc.new {|arg| puts self} #self main here
    >
    > test_obj.instance_eval do
    > puts @instance_variable #test_obj instance variable.
    > puts outside #any variable from outside the block can be passed in.
    > puts proc #will return main rather than the object
    > end
    >
    > Hmm, I get the feeling I have ranted about stuff that does not help you
    > one bit with your question...
    >
    > Get back to me with a bit more detail and hopefully I'll be able to
    > help!
    >
    > Cheers,
    >
    > Matt.



    Oops, that last 'puts proc' should be proc.call("whatever")

    If full:

    class Test
    def initialize(arg)
    @instance_variable = arg
    end
    end

    test_obj = Test.new("initializing_arg")

    outside = "outside instance_eval"

    proc = Proc.new {|arg| puts self} #self main here

    test_obj.instance_eval do
    puts @instance_variable #test_obj instance variable.
    puts outside #any variable from outside the block can be passed in.
    proc.call("whatever") #will return main rather than the object
    end


    --
    Posted via http://www.ruby-forum.com/.
    Matt Gretton, May 20, 2007
    #3
  4. Ruby Talk

    Phrogz Guest

    On May 19, 10:07 pm, Ruby Talk <> wrote:
    > self.instance_eval &block
    >
    > And calling a block, passing it parameters is easy enough:
    >
    > block.call(*params)
    >
    > But is there some way to do both? To both change the scope of evaluation
    > and pass variables into the block.


    http://eigenclass.org/hiki.rb?instance_exec
    Phrogz, May 20, 2007
    #4
  5. Ruby Talk

    Robert Dober Guest

    On 5/20/07, Phrogz <> wrote:
    > On May 19, 10:07 pm, Ruby Talk <> wrote:
    > > self.instance_eval &block
    > >
    > > And calling a block, passing it parameters is easy enough:
    > >
    > > block.call(*params)
    > >
    > > But is there some way to do both? To both change the scope of evaluation
    > > and pass variables into the block.

    >
    > http://eigenclass.org/hiki.rb?instance_exec
    >
    >
    >

    OP We have just discussed this twice recently, maybe you should browse
    the recent posts...

    Phrogz: Your link seems outdated,
    c.f.http://eigenclass.org/hiki/bounded space instance_exec
    for a discussion of #undef_method (bad) vs. #remove_method (good) and
    please note the potential danger of endless recursion.

    Cheers
    Robert


    --
    You see things; and you say Why?
    But I dream things that never were; and I say Why not?
    -- George Bernard Shaw
    Robert Dober, May 20, 2007
    #5
    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. Paul Opal
    Replies:
    12
    Views:
    934
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    654
    Patricia Shanahan
    Sep 13, 2005
  3. morrell
    Replies:
    1
    Views:
    938
    roy axenov
    Oct 10, 2006
  4. Steven T. Hatton
    Replies:
    9
    Views:
    465
  5. Steve V
    Replies:
    6
    Views:
    234
    Steve V
    Apr 20, 2005
Loading...

Share This Page