How to call method chaining using map method in ruby?
3 answers
If you really want to bind methods, you can override the # to_proc symbol
* This answer is similar to the one voted. I wonder why...
class Symbol
def to_proc
to_s.to_proc
end
end
class String
def to_proc
split("\.").to_proc
end
end
class Array
def to_proc
proc{ |x| inject(x){ |a,y| a.send(y) } }
end
end
strings_arr = ["AbcDef", "GhiJkl", "MnoPqr"]
strings_arr.map(&:"underscore.upcase")
#=> ["ABC_DEF", "GHI_JKL", "MNO_PQR"]
strings_arr.map(&"underscore.upcase")
#=> ["ABC_DEF", "GHI_JKL", "MNO_PQR"]
strings_arr.map(&["underscore", "upcase"])
#=> ["ABC_DEF", "GHI_JKL", "MNO_PQR"]
0
source to share