public
Last active

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12
# 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
gistfile2.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.