Question

I currently have this code

def objects(ids)
  array = []

  ids.each do |id|
    array << object(id) # => #<object[id]>
  end

  array
end

objects([1, 2, 3])
# => [#<object1>, #<object2>, #<object3>]

It seems like there should be a cleaner way to do this. Can anyone help?

Was it helpful?

Solution

EDIT This is what works

[1, 2, 3].map do |id|
  object(id)
end

ORIGINAL go this way:

[1, 2, 3].map(&:object_id)
# => [3, 5, 7]

def objects(ids)
  ids.map(&:object_id)
end

objects([1, 2, 3])
# => [3, 5, 7]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top