Skip to content

Instantly share code, notes, and snippets.

@jnicklas
Created May 21, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnicklas/843e87ec962a2ed85b4a to your computer and use it in GitHub Desktop.
Save jnicklas/843e87ec962a2ed85b4a to your computer and use it in GitHub Desktop.
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"]
@henrik
Copy link

henrik commented May 21, 2015

IIRC underscore or lodash or something has a syntax like myArray.filter(length: 3) Sooo convenient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment