From: chiaro scuro Date: 2006-04-27T23:43:17+09:00 Subject: Re: from block to code (also: how to get the bindings of a block) The quiz is a great resource, thanks. In this case it happens that I can ignore closures. However, I don't need closures I don't see why Ruby shouldn't implement a method to return the code of a block.. On 4/27/06, Dave Burt wrote: > chiaro scuro wrote: > >>> Does anyone know how to convert a block to a string representation of > >>> the code it contains? > >> Read the source file? > > how would I know what is the source file and lines associated with a block? > > > > I was really hoping to get the info from the runtime, but I guess that > > what you suggest could also be a pragmatic option. > > OK, here are some practical options. (Sorry for the previous comment. I > was trying to dig this stuff up from my memory, but Quiz 34 came up > instead.) > > #1: You can store a Proc with its code if you're willing to forgo > closure of the the environment and to use syntax like this: > > method_name %{ > proc_code > } > > See Ruby Quiz 38: SerializableProc - http://rubyquiz.com/quiz38.html > > #2: You can get it from (I think) the runtime, using ZenHacks' > Proc.to_ruby. See Ryan Davis' post in response to the same quiz: > http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/148082 > > Cheers, > Dave > > -- Chiaroscuro --- Liquid Development: http://liquiddevelopment.blogspot.com/