Last active
July 31, 2017 13:40
-
-
Save walski/167262bae0b93b86d458c8c1d805daa2 to your computer and use it in GitHub Desktop.
Mapping without the curly braces ;)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MethodChainer < BasicObject | |
def initialize(obj, deep_chain = false) | |
@obj = obj | |
@deep_chain = deep_chain | |
end | |
def __ | |
@obj.map { |e| e } | |
end | |
def method_missing(name, *args) | |
result = (@obj.map { |e| e.send(name, *args) }) | |
return result unless @deep_chain | |
result.__ | |
end | |
end | |
module Enumerable | |
def _ | |
map._ | |
end | |
def __ | |
map.__ | |
end | |
end | |
class Enumerator | |
def _ | |
MethodChainer.new(self) | |
end | |
def __ | |
MethodChainer.new(self, deep_chain: true) | |
end | |
end | |
a = ['Yeah',2,'Hello WoRlD'] | |
a.map._.to_s # => ["Yeah", "2", "Hello WoRlD"] | |
a._.to_s # => ["Yeah", "2", "Hello WoRlD"] | |
a.map.__.to_s.downcase.reverse.__ # => ["haey", "2", "dlrow olleh"] | |
a.__.to_s.downcase.reverse.__ # => ["haey", "2", "dlrow olleh"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment