Accessing code blocks

Discussion in 'Ruby' started by Grzegorz Dostatni, Jul 8, 2003.

  1. Cheers!

    I need to do something silly. I need to access the code block as a
    variable (to pass it - without evaluating - to another function). How can
    I do that?

    Is it possible for me to access the "text version of block?" Can I print
    out the code that has been attached to my function instead of executing
    it?

    The only functions that work on code block (that I could find) are:
    block_given? and yield. Obviously they do not fit the bill...

    Please help.

    Grzegorz Dostatni
    Grzegorz Dostatni, Jul 8, 2003
    #1
    1. Advertising

  2. On 2003-07-09 06:05:42 +0900, Grzegorz Dostatni wrote:
    > Please help.


    Try this:

    def foo(&bar) bar end

    bar = foo { |x| x + 1 }

    bar.call 1 # => 2


    --
    5) "I have challenged the entire quality assurance team to a Bat-Leth
    contest. They will not concern us again."
    -- Top 12 things likely to be overheard if you had a Klingon Programmer
    Florian Frank, Jul 8, 2003
    #2
    1. Advertising

  3. On Wed, Jul 09, 2003 at 06:05:42AM +0900, Grzegorz Dostatni wrote:
    > I need to do something silly. I need to access the code block as a
    > variable (to pass it - without evaluating - to another function). How can
    > I do that?


    It's not silly at all, but the syntax isn't obvious if you haven't seen it
    before.

    At the end of your argument list give a named argument preceded with '&'.
    This converts the block to an explicit Proc object which you can pass
    around.

    def function1(&blk)
    function2(blk)
    end

    def function2(blk)
    blk.call(99)
    end

    function1 { |i| puts i } #>> prints 99


    > Is it possible for me to access the "text version of block?" Can I print
    > out the code that has been attached to my function instead of executing
    > it?


    No, decompiling blocks is not possible AFAIK :)

    Regards,

    Brian.
    Brian Candler, Jul 9, 2003
    #3
  4. Grzegorz Dostatni

    Kent Dahl Guest

    Grzegorz Dostatni wrote:
    > Is it possible for me to access the "text version of block?" Can I print
    > out the code that has been attached to my function instead of executing
    > it?


    As others have pointed out, this is not possible, atleast not without
    jumping through hoops. If you don't mind jumping through hoops on the
    caller side and a little uglier code, here is a quick stab I did at this
    earlier:

    class X
    def first_definition( &binding )
    @binding ||= binding
    source = binding.call
    redefine( source )
    end
    def redefine( source )
    @source = source
    @block = eval "Proc.new { #{source} }", @binding
    end
    def x(i)
    @block.call(i)
    end
    end
    x = X.new
    a = 3
    x.first_definition { %{|i|a+i} }
    puts x.x(2) #=> 5
    x.redefine %{|i|a-i}
    puts x.x(2) #=> 1

    Notice the syntax { %{|i|a+i} }
    I wrap a string, "|i|a+i", inside a block. The block at this level only
    serves as a binding, the contents of the string is used to create the
    block itself. HTH

    --
    (\[ Kent Dahl ]/)_ _~_ _____[ http://www.pvv.org/~kentda/ ]_____/~
    ))\_student_/(( \__d L b__/ (pre-) Master of Science in Technology )
    ( \__\_õ|õ_/__/ ) _)Industrial economics and technological management(
    \____/_ö_\____/ (____engineering.discipline_=_Computer::Technology___)
    Kent Dahl, Jul 9, 2003
    #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. Arjen
    Replies:
    3
    Views:
    436
    Scott Allen
    Feb 27, 2005
  2. M_at
    Replies:
    3
    Views:
    5,718
  3. Microsoft

    Controls that contain code: Code Blocks Not Supported

    Microsoft, Dec 14, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    413
    Anthony Williams
    Dec 14, 2005
  4. matt
    Replies:
    1
    Views:
    254
    George Ogata
    Aug 6, 2004
  5. Steven Taylor
    Replies:
    9
    Views:
    247
    Brian Candler
    Apr 27, 2009
Loading...

Share This Page