Skip to content

Instantly share code, notes, and snippets.

@ngauthier
Created November 21, 2011 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ngauthier/1383631 to your computer and use it in GitHub Desktop.
Save ngauthier/1383631 to your computer and use it in GitHub Desktop.
currying an instance method
class Foo
def foo(x)
"foo! #{x}"
end
def bar
# my imaginary syntax for "create functional reference from method foo with argument bar.
&(:foo, 5)
end
end
# Foo.new.bar.call
# => "foo! 5"
@tenderlove
Copy link

Just return a lambda?

class Foo
  def foo x
    "foo! #{x}"
  end

  def bar
    -> { foo 5 }
  end
end

Foo.new.bar.call

@ngauthier
Copy link
Author

ah yeah I thought lambda's didn't keep scope, but they do. Is it procs that don't keep scope? Or you you have to manually bind to change scope?

Thanks.

@rubiii
Copy link

rubiii commented Nov 21, 2011

closures always keep their scope?! but you can "ignore" their original binding and evaluate them in a different context if you like.

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