[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

capture code in a block

kristoph

10/19/2007 11:10:00 PM

Greetings,

How might I capture the code in a block so that I can serialize it for
later use? So for example ...

file = CodeFile.new( 'something.rb')

file.capture do
puts 'Hello World'
end

.... which prompts a file to be created with ...

puts 'Hello World'

Any thoughts how this might be accomplished?

]{

2 Answers

Konrad Meyer

10/20/2007 12:47:00 AM

0

Quoth Kristoph:
> Greetings,
>
> How might I capture the code in a block so that I can serialize it for
> later use? So for example ...
>
> file = CodeFile.new( 'something.rb')
>
> file.capture do
> puts 'Hello World'
> end
>
> ... which prompts a file to be created with ...
>
> puts 'Hello World'
>
> Any thoughts how this might be accomplished?

Do you actually want a literal "puts 'Hello World'" in the file, or just the
string "Hello World"?

def my_method &block
@block = block
end

def my_other_method
@block.call
end

Something like that?

HTH,
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

kristoph

10/20/2007 5:38:00 PM

0

Konrad,

Yes I want to capture and serialize the actual code into a file (and
not actually execute that code until some later juncture).

Basically I just want the contents of a block as a string, without
executing it.

Kristoph

On Oct 19, 5:46 pm, Konrad Meyer <kon...@tylerc.org> wrote:
> Quoth Kristoph:
>
>
>
> > Greetings,
>
> > How might I capture the code in a block so that I can serialize it for
> > later use? So for example ...
>
> > file = CodeFile.new( 'something.rb')
>
> > file.capture do
> > puts 'Hello World'
> > end
>
> > ... which prompts a file to be created with ...
>
> > puts 'Hello World'
>
> > Any thoughts how this might be accomplished?
>
> Do you actually want a literal "puts 'Hello World'" in the file, or just the
> string "Hello World"?
>
> def my_method &block
> @block = block
> end
>
> def my_other_method
> @block.call
> end
>
> Something like that?
>
> HTH,
> --
> Konrad Meyer <kon...@tylerc.org>http://konrad.sobertil...
>
> signature.asc
> 1KDownload