I have an array of hashes, each hash has two keys : “from” and “to”
@dictionary = [{:to=>"lazy", :from=>"quick"}, {:to=>"flies", :from=>"jumps"}, {:from => "over the", :to => "under the"}]
I have some long string
@text = "quick brown fox jumps over the lazy dog"
How can I replace all occurences of “from” sentences to “to” sentences of the dictionary ?
The output should be:
lazy brown fox flies under the lazy dog
What is the most efficient way ?
Or if you’d prefer it on a single line:
It’s the exact same code, just using
{ }instead ofdo endfor the block (which tends to be the general Ruby practice).