Especially when working with structs, it would be nice to be able to call a different method per each element in an array, something like this:
array = %w{name 4 tag 0.343}
array.convert(:to_s, :to_i, :to_sym, :to_f)
# => ["name", 4, :tag, 0.343]
Are there any simple one-liners, ActiveSupport methods, etc. to do this easily?
I think it can be done as below way also :-
array = %w{name 4 tag 0.343}
class Array
def convert(*args)
self.zip(args).map { |string, meth| string.public_send(meth) }
end
end
array.convert(:to_s, :to_i, :to_sym, :to_f) # => ["name", 4, :tag, 0.343]