Skip to main content
added 1 characters in body
Source Link

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.

You can do this:

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

This preserves the value if it is nonzero.

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.

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.

added 1 characters in body
Source Link

You can do this:

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

This preserves the value if it is nonzero.

Code review

Note that when you initialize a return variable and anything other than 0 will be considered truethen loop over another variable, e. It's interestingg.

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

A more idiomatic Ruby way to note that v.nonzero? wouldn't work indo this case because it doesn't returnis with a boolean (it returnsmap, as the number or nil)answers show. This will in general result in more concise, expressive code.

You can do this:

Hash[ preferences.map {|k,v| [k.to_sym, !v.zero?]} ]

and anything other than 0 will be considered true. It's interesting to note that v.nonzero? wouldn't work in this case because it doesn't return a boolean (it returns the number or nil).

You can do this:

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

This preserves the value if it is nonzero.

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.

added 1 characters in body
Source Link

You can do this:

Hash[ preferences.map {|k,v| [k.to_sym, !selfv.zero?]} ]

and anything other than 0 will be considered true. It's interesting to note that v.nonzero? wouldn't work in this case because it doesn't return a boolean (it returns the number or nil).

You can do this:

Hash[ preferences.map {|k,v| [k.to_sym, !self.zero?]} ]

and anything other than 0 will be considered true.

You can do this:

Hash[ preferences.map {|k,v| [k.to_sym, !v.zero?]} ]

and anything other than 0 will be considered true. It's interesting to note that v.nonzero? wouldn't work in this case because it doesn't return a boolean (it returns the number or nil).

Post Undeleted by Mark Thomas
added 1 characters in body
Source Link
Loading
Post Deleted by Mark Thomas
Source Link
Loading