Skip to content

Instantly share code, notes, and snippets.

@rkh
Created February 21, 2010 17:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkh/310410 to your computer and use it in GitHub Desktop.
Save rkh/310410 to your computer and use it in GitHub Desktop.
# http://yehudakatz.com/2010/02/21/ruby-is-not-a-callable-oriented-language/
module DoAsYouPlease
Object.send :include, self
def method_missing(name, *args, &block)
self.class.const_get(name).call(*args, &block)
end
end
Foo = proc { 42 }
Foo # => #<Proc:...>
Foo() # => 42
def callable_oriented(&block)
scope = block.binding
eigenclass = class << scope.eval("self"); self; end
method_catcher = Module.new do
define_method(:method_missing) do |name, *args|
name = name.to_s
return super(name.to_sym, *args) unless name =~ /^[A-Z]/ or scope.eval("local_variables").any? { |n| n.to_s == name }
scope.eval(name).call(*args)
end
end
eigenclass.send :include, method_catcher
yield
end
foo = proc { 42 }
callable_oriented do
p foo
p foo()
end
@shime
Copy link

shime commented Jun 26, 2013

mind = 💥

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