From: Brian Candler Date: 2011-11-02T05:01:08+09:00 Subject: Re: Working from a string to an array Here's a version which doesn't split the string into parts: >> hash = { ?> "bob"=> 42, ?> "joe"=> 1, ?> "tom"=> 16, ?> "luc"=> 872, ?> "mike"=> 32, ?> } => {"mike"=>32, "joe"=>1, "luc"=>872, "tom"=>16, "bob"=>42} >> def transform(str, hash) >> str.gsub(/@(\w+)/) { hash[$1] } >> end => nil >> transform("Hello @bob!", hash) => "Hello 42!" But it's not much harder to meet your original requirement: >> def transform2(str, hash) >> str.split(/(@\w+)/).map { |x| x =~ /^@(.+)/ ? hash[$1] : x } >> end => nil >> transform2("Hello @bob!", hash) => ["Hello ", 42, "!"] -- Posted via http://www.ruby-forum.com/.