From: Kent Dahl Date: 2003-07-09T19:10:18+09:00 Subject: Re: Accessing code blocks 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___)