Skip to content

@rkh /gist:310410
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
# 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

mind = :boom:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.