How to call different methods as given in an array

来源:互联网 发布:sql 2005 64位破解版 编辑:程序博客网 时间:2024/05/21 03:56

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) }  endend    array.convert(:to_s, :to_i, :to_sym, :to_f) # => ["name", 4, :tag, 0.343]

0 0