Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   capture code in a block (http://www.velocityreviews.com/forums/t845022-capture-code-in-a-block.html)

Kristoph 10-19-2007 11:10 PM

capture code in a block
 
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?

]{


Konrad Meyer 10-20-2007 12:46 AM

Re: capture code in a block
 
--nextPart2175644.v5JOTTBBfD
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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


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

def my_method &block
@block =3D block
end

def my_other_method
@block.call
end

Something like that?

HTH,
=2D-=20
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/

--nextPart2175644.v5JOTTBBfD
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBHGU/+CHB0oCiR2cwRAtv6AJ0fEwmouk6VwJtzqXA+Xf5p34gg2QCeP Sev
7cyCXm2/+L7vbmxmivYoFfk=
=L7UK
-----END PGP SIGNATURE-----

--nextPart2175644.v5JOTTBBfD--


Kristoph 10-20-2007 05:38 PM

Re: capture code in a block
 
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.sobertillnoon.com/
>
> signature.asc
> 1KDownload





All times are GMT. The time now is 05:14 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.