Ruby if shorthand + map shorthand -
how use map(&:name) shorthand inside a
condition ? map(&:name) : name
if shorthand?
i got syntax error, ruby trying evaluate first colon.
it's unclear problem here way use "map(&:name)
" inside conditional:
require 'ostruct' # demonstration using openstruct. arr = [openstruct.new(:name => 'foo'), openstruct.new(:name => 'bar'), openstruct.new(:name => 'gah')] name = 'testing' (true ? arr.map(&:name) : name) # => ["foo", "bar", "gah"] (false ? arr.map(&:name) : name) # => "testing"
Comments
Post a Comment