Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

map method with mutated iterator

I have an array of objects I want to iterate through with map-

object_array.map {|o| o.dostuff }

But I want o iterator to be a string representation of the object, so I have to do something like this-

object_array.map do |o|
  o = o.to_s
  o.dostringstuff
end

Is there any way to do it in one line? (intrepreter doesn't accept this)

object_array.map {|o.to_s| o.dostringstuff }
like image 789
Akra Avatar asked Sep 14 '26 10:09

Akra


1 Answers

Why not chain maps, especially with a shorthand syntax via Symbol#to_proc?

object_array.map(&:to_s).map(&:dostringstuff)
like image 176
Oleksii Filonenko Avatar answered Sep 16 '26 03:09

Oleksii Filonenko