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
- How to call different methods as given in an array
- How to sum values in an array with different hash
- How to define an array in c#.
- How to initialize an array in C
- How to format strings in an array in ruby?
- How to group anagrams in a string into an array
- Different methods to call Web Services from AJAX
- Divide and Conquer (1) -- Kth Largest Element in an Array,Different Ways to Add Parentheses
- Different ways how to escape an XML string in C# (zz)
- Given an array of characters. How would you reverse it.
- HOW TO: Find the Problem Exception Stack When You Receive an UnhandledExceptionFilter Call in the St
- HOW TO: Find the Problem Exception Stack When You Receive an UnhandledExceptionFilter Call in the St
- how to remove nil and blank string in an array in Ruby
- How to sort an array in Ruby to a particular order?
- How to Run a Program as an Administrator in Windows 7
- How to Run a Program as an Administrator in Windows 7
- How to run an ajax call after another ajax call?
- How to remove Duplicates from an Array
- c语言设置控制台背景色和前景色
- Android漫游记(5)---ARM GCC 内联汇编烹饪书(附实例分析)
- IOS之sha加密、md5常规加密、md5二次加密详解及示例程序
- mysql中文手册
- 【贪心算法】今年暑假不AC
- How to call different methods as given in an array
- Android开发中如何调用摄像头的功能
- Vi下删除SWP文件
- MongoDB 一对多关系建模
- VC RichEdit删除不了内容问题
- Scrapy spider代码片段
- Cocos2d-X之LUA笔记
- OCP 1Z0 052 171
- JS中的attribute和property的区别和联系