Reputation: 632
How do I use a map(&:name) shorthand inside a
condition ? map(&:name) : name
if shorthand?
I got a syntax error, ruby is trying to evaluate the first colon.
Upvotes: 0
Views: 814
Reputation: 156444
It's unclear exactly what your problem is but here is a way to use "map(&:name)
" inside a conditional:
require 'ostruct' # For 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"
Upvotes: 1