From: Josh Cheek Date: 2009-11-20T05:56:58+09:00 Subject: Re: Ternary Operator and Arrays --000e0cd294e83e7f430478bf99f9 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Nov 19, 2009 at 12:29 PM, Marnen Laibow-Koser wrote: > > It's probably a precedence issue. Looks like << is binding tighter than > ?: . > > Anyway, you can just do ary = another_array.collect{|x| some_hash[x] || > ' '} > > You have to be careful with this, though, because nil and false could be valid values. ary = Array.new some_hash = { 'a' => 1 , 'b' => 2 , 'c' => nil , 'd' => false } another_array = [ 'a' , 'c' , 'd' , 'e' ] #if you want nil and false to be valid values, this doesn't do what you expect another_array.collect{|x| some_hash[x] || ' ' } # => [1, " ", " ", " "] #you can set the default to the string, then it will return that if the key doesn't exist some_hash.default = ' ' another_array.collect{ |x| some_hash[x] } # => [1, nil, false, " "] --000e0cd294e83e7f430478bf99f9--