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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -