Created
May 21, 2015 14:21
-
-
Save jnicklas/843e87ec962a2ed85b4a to your computer and use it in GitHub Desktop.
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 Underscore < BasicObject | |
def initialize(invocations = []) | |
@invocations = invocations | |
end | |
def to_proc | |
invocations = @invocations | |
::Kernel.lambda do |val| | |
invocations.reduce(val) do |val, (name, args, block)| | |
val.send(name, *args, &block) | |
end | |
end | |
end | |
def respond_to_missing?(*) | |
true | |
end | |
def method_missing(name, *args, &block) | |
::Underscore.new(@invocations + [[name, args, block]]) | |
end | |
end | |
_ = Underscore.new | |
["foo", "bar", "quox"].map(&_.gsub("oo", "aa")).select(&_.length <= 3) # => ["faa", "bar"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IIRC underscore or lodash or something has a syntax like
myArray.filter(length: 3)
Sooo convenient.