T
Thufir
"code_words.each do |real, code|
idea.gsub!( real, code )
end
You see the each method? The each method is all over in Ruby. It's
available for Arrays, Hashes, even Strings. Here, our code_words
dictionary is kept in a Hash. This each method will hurry through all
the pairs of the Hash, one dangerous word matched with its code word,
handing each pair to the gsub! method for the actual replacement."
from page 33 of whys-poignant-guide-to-ruby.pdf
Is this similar to nested for statements? I don't think so. In the
first line, why are both "real" and "code" part of the interation?
Why would this fail:
code_words.each do |real|
idea.gsub!( real, code )
end
wouldn't the corresponding code get looked up by during the loop? Or,
how could the above be changed so that it would work?
thanks,
Thufir
idea.gsub!( real, code )
end
You see the each method? The each method is all over in Ruby. It's
available for Arrays, Hashes, even Strings. Here, our code_words
dictionary is kept in a Hash. This each method will hurry through all
the pairs of the Hash, one dangerous word matched with its code word,
handing each pair to the gsub! method for the actual replacement."
from page 33 of whys-poignant-guide-to-ruby.pdf
Is this similar to nested for statements? I don't think so. In the
first line, why are both "real" and "code" part of the interation?
and then find the corresponding bit of the hash.From my understanding of a hash, you can iterate through the keys only
Why would this fail:
code_words.each do |real|
idea.gsub!( real, code )
end
wouldn't the corresponding code get looked up by during the loop? Or,
how could the above be changed so that it would work?
thanks,
Thufir