Scope of block

Discussion in 'Ruby' started by daniel, Dec 10, 2006.

  1. daniel

    daniel Guest

    Hello i have a question about scopes of block
    I want to define a block at one time.
    But when it get's called it should have the scope (vars ed )
    of the calling environement

    Example :
    ------------------------------

    class BlockTest

    attr_accessor :myblock

    def process(&block)
    self.myblock = block
    end
    end


    t = BlockTest.new
    t.process {

    "var = #{out_of_scope_var}"
    }

    out_of_scope_var = "daniel"
    p t.myblock.call

    Results in :

    blocktest.rb:15: undefined local variable or method `out_of_scope_var'
    for main:Object (NameError) from blocktest.rb:19

    How can i call the proc so that the out_of_scope_var is known :

    result would be :

    so i would get :

    daniel-wijnands-powerbook58-2:~/jim/vendor/plugins/widget_grid/lib
    danielwijnands$ ruby blocktest.rb
    "var = daniel"
     
    daniel, Dec 10, 2006
    #1
    1. Advertising

  2. daniel

    daniel Guest

    Paul Lutus schreef:

    >
    > Perhaps you will be satisfied that the block receive values from the parent
    > environment. This is the normal way to solve the problem.
    >


    That is how it is solved now, but the proc needs to be flexible.
    I call much proc's and i don't now what to pass.
    It are not just vars that i want to accessible but also methods ed

    Any idea ?
     
    daniel, Dec 10, 2006
    #2
    1. Advertising

  3. daniel

    Gustav Paul Guest

    daniel wrote:
    > Hello i have a question about scopes of block
    > I want to define a block at one time.
    > But when it get's called it should have the scope (vars ed )
    > of the calling environement
    >
    > Example :
    > ------------------------------
    >
    > class BlockTest
    >
    > attr_accessor :myblock
    >
    > def process(&block)
    > self.myblock = block
    > end
    > end
    >
    >
    > t = BlockTest.new
    > t.process {
    >
    > "var = #{out_of_scope_var}"
    > }
    >
    > out_of_scope_var = "daniel"
    > p t.myblock.call
    >
    > Results in :
    >
    > blocktest.rb:15: undefined local variable or method `out_of_scope_var'
    > for main:Object (NameError) from blocktest.rb:19
    >
    > How can i call the proc so that the out_of_scope_var is known :
    >
    > result would be :
    >
    > so i would get :
    >
    > daniel-wijnands-powerbook58-2:~/jim/vendor/plugins/widget_grid/lib
    > danielwijnands$ ruby blocktest.rb
    > "var = daniel"
    >
    >
    >
    >

    The following seems to work:
    #########
    class BlockTest
    def process(&block)
    @block=block
    end

    def myblock
    instance_eval{@block}.call
    end
    end

    out_of_scope_var = "out of scope, me"
    t = BlockTest.new
    t.process {"var = #{out_of_scope_var}"}

    p t.myblock
    #=> "var = out of scope, me"
    ###########
    Hope this helps,
    Gustav Paul
     
    Gustav Paul, Dec 10, 2006
    #3
  4. daniel

    Guest

    Hi --

    On Mon, 11 Dec 2006, Gustav Paul wrote:

    > The following seems to work:
    > #########
    > class BlockTest
    > def process(&block)
    > @block=block
    > end
    >
    > def myblock
    > instance_eval{@block}.call


    No need for instance_eval there. @block already belongs to self, and
    the instance_eval just resets self to self :)


    David

    --
    David A. Black |
    Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
    DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
    [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
    [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
     
    , Dec 10, 2006
    #4
  5. daniel

    Gustav Paul Guest

    wrote:
    > Hi --
    >
    > On Mon, 11 Dec 2006, Gustav Paul wrote:
    >
    >> The following seems to work:
    >> #########
    >> class BlockTest
    >> def process(&block)
    >> @block=block
    >> end
    >>
    >> def myblock
    >> instance_eval{@block}.call

    >
    > No need for instance_eval there. @block already belongs to self, and
    > the instance_eval just resets self to self :)
    >
    >
    > David
    >


    LOL!
    doh!
    :-]
     
    Gustav Paul, Dec 10, 2006
    #5
  6. daniel

    daniel Guest

    Gustav Paul schreef:

    > The following seems to work:
    > #########
    > class BlockTest
    > def process(&block)
    > @block=block
    > end
    >
    > def myblock
    > instance_eval{@block}.call
    > end
    > end
    >
    > out_of_scope_var = "out of scope, me"
    > t = BlockTest.new
    > t.process {"var = #{out_of_scope_var}"}
    >
    > p t.myblock
    > #=> "var = out of scope, me"
    > ###########
    > Hope this helps,
    > Gustav Paul


    Hi paul, i know that's works, but i need it slightly differtent
    because you set out_of_scope_var before you create the block
    I want it to be accessible when you set it after you create the block
    With your method it is in the scope of the block
    I want the block to have access to the scope when you call it
    So this should work :


    class BlockTest
    def process(&block)
    @block=block
    end
    def myblock
    instance_eval{@block}.call
    end
    end


    t = BlockTest.new
    t.process {"var = #{out_of_scope_var}"}



    out_of_scope_var = "out of scope, me"
    p t.myblock


    But that gives :

    blocktest.rb:40: undefined local variable or method `out_of_scope_var'
    for main:Object (NameError)
    from blocktest.rb:34:in `myblock'
    from blocktest.rb:45

    :( thanks anyway :)
     
    daniel, Dec 10, 2006
    #6
  7. daniel

    NoonKnight Guest

    you could add an eval() into your example code, so that:

    t = BlockTest.new
    t.process { "var = #{out_of_scope_var}" }
    out_of_scope_var = "daniel"
    p t.myblock.call

    would become:

    t = BlockTest.new
    t.process { "var = #{eval('out_of_scope_var'}" }
    out_of_scope_var = "daniel"
    p t.myblock.call

    hope that helps. --noon

    daniel wrote:
    > Hello i have a question about scopes of block
    > I want to define a block at one time.
    > But when it get's called it should have the scope (vars ed )
    > of the calling environement
    >
    > Example :
    > ------------------------------
    >
    > class BlockTest
    >
    > attr_accessor :myblock
    >
    > def process(&block)
    > self.myblock = block
    > end
    > end
    >
    >
    > t = BlockTest.new
    > t.process {
    >
    > "var = #{out_of_scope_var}"
    > }
    >
    > out_of_scope_var = "daniel"
    > p t.myblock.call
    >
    > Results in :
    >
    > blocktest.rb:15: undefined local variable or method `out_of_scope_var'
    > for main:Object (NameError) from blocktest.rb:19
    >
    > How can i call the proc so that the out_of_scope_var is known :
    >
    > result would be :
    >
    > so i would get :
    >
    > daniel-wijnands-powerbook58-2:~/jim/vendor/plugins/widget_grid/lib
    > danielwijnands$ ruby blocktest.rb
    > "var = daniel"
     
    NoonKnight, Dec 11, 2006
    #7
  8. daniel

    NoonKnight Guest

    oops, i forgot to put the closing paren. should be:

    t.process { "var = #{eval('out_of_scope_var')}" }

    --noon
     
    NoonKnight, Dec 11, 2006
    #8
    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:
    963
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    676
    Patricia Shanahan
    Sep 13, 2005
  3. morrell
    Replies:
    1
    Views:
    968
    roy axenov
    Oct 10, 2006
  4. Steven T. Hatton
    Replies:
    9
    Views:
    492
  5. Steve V
    Replies:
    6
    Views:
    241
    Steve V
    Apr 20, 2005
Loading...

Share This Page