From: Robert Klemme Date: 2006-12-04T20:40:09+09:00 Subject: Re: Getting the source code of a block On 04.12.2006 12:20, Peter Szinek wrote: > Is the following thing possible somehow? > > def foo(&bar) > do_something_with(bar) > end > > baz = foo { puts 'hello'; i +=1; fluff(ork) } > > now, baz should contain the string > > "puts 'hello'; i +=1; fluff(ork)" > > since I need to do this in the same file where I am defining the block, > I could get it using __FILE__ + some regexps - just asking if there is > some more elegant way... No. Alternatively you could cook your own irb(main):001:0> def pproc(code) irb(main):002:1> block = eval("lambda {#{code}}") irb(main):003:1> class < block irb(main):005:1> end => nil irb(main):006:0> x = pproc '|a,b| a+b' => # irb(main):007:0> x[10,20] => 30 irb(main):008:0> x.source_code => "|a,b| a+b" Where do you need that for? Kind regards robert