Skip to main content
5 of 5
added 1 characters in body

You can do this:

Hash[ preferences.map {|k,v| [k.to_sym, v.nonzero? or false]} ]

This preserves the value if it is nonzero, otherwise makes it false. It does not change a 1 to true though. I like @tokland's answer best, because it not only does the exact transformation you want, but makes it easy to extend and use as a general purpose transformer (if you ever need to change other values as well), which one might assume is your purpose for this method, based on the name.

Code review

Note that when you initialize a return variable and then loop over another variable, e.g.

hsh = {}
preferences.each do
  #something that updates hsh
end

A more idiomatic Ruby way to do this is with a map, as the answers show. This will in general result in more concise, expressive code.