P
Pete Elmore
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I haven't been able to find any documentation on the syntax for a
certain feature, and am not sure the feature exists. What I would like
to do is add a block of code to a hash, so I could do something like this:
state = 0
states = {
0 => { puts "State 0"; state = 1 },
1 => { puts "State 1"; state = 0 }
}
However, I haven't found any documentation on doing anything like this,
so I'm not sure you can. What I'm doing right now is a little ugly:
state = 0
states = {
0 => "puts 'State 0'; state = 1",
1 => "puts 'State 1'; state = 0"
}
while a_condition
eval states[state]
end
Obviously, keeping code as a string and then using eval gets pretty
hairy if it gets any more complicated than this. The ability to pass
blocks as data is one of Ruby's best features, and this would allow
programmers to avoid constructs like
state = X
data.each { |datum|
if state == 0
code
elsif state == 1
other code
...
}
(If this is repugnant to the nature of Ruby, there's a better way to do
it, or it already exists and I've missed it, please forgive my n00b
question!)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Debian - http://enigmail.mozdev.org
iD8DBQFBgu+Yv24lB609Ih8RAvYhAKCSYQyZcpT/nj6LJJCBlxKHhkceUACfVca5
hrLw6eGQwZ7XZh2B2an/toc=
=uKEd
-----END PGP SIGNATURE-----
Hash: SHA1
I haven't been able to find any documentation on the syntax for a
certain feature, and am not sure the feature exists. What I would like
to do is add a block of code to a hash, so I could do something like this:
state = 0
states = {
0 => { puts "State 0"; state = 1 },
1 => { puts "State 1"; state = 0 }
}
However, I haven't found any documentation on doing anything like this,
so I'm not sure you can. What I'm doing right now is a little ugly:
state = 0
states = {
0 => "puts 'State 0'; state = 1",
1 => "puts 'State 1'; state = 0"
}
while a_condition
eval states[state]
end
Obviously, keeping code as a string and then using eval gets pretty
hairy if it gets any more complicated than this. The ability to pass
blocks as data is one of Ruby's best features, and this would allow
programmers to avoid constructs like
state = X
data.each { |datum|
if state == 0
code
elsif state == 1
other code
...
}
(If this is repugnant to the nature of Ruby, there's a better way to do
it, or it already exists and I've missed it, please forgive my n00b
question!)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Debian - http://enigmail.mozdev.org
iD8DBQFBgu+Yv24lB609Ih8RAvYhAKCSYQyZcpT/nj6LJJCBlxKHhkceUACfVca5
hrLw6eGQwZ7XZh2B2an/toc=
=uKEd
-----END PGP SIGNATURE-----