Skip to content

Instantly share code, notes, and snippets.

@mbj
Created October 21, 2012 20:26
Show Gist options
  • Save mbj/3928397 to your computer and use it in GitHub Desktop.
Save mbj/3928397 to your computer and use it in GitHub Desktop.
Ruby imperfectness?
module ModuleWithMethod
def the_method
end
end
class ModuleWithContext < ::Module
def initialize(context)
@context = context
end
def the_method_with_context
@context
end
end
class Foo
extend ModuleWithMethod
extend ModuleWithContext.new(:the_context)
p method(:the_method)
p method(:the_method_with_context)
end
# A: Somewhere deep into ruby is a def extend(mod); return unless mod.class == ::Module; make_methods_available_from(mod); end
# B: I understood something fundamental in the wrong way.
#<Method: Class(ModuleWithMethod)#the_method>
NameError: undefined method `the_method_with_context' for class `#<Class:0x2dc0435>'
method at org/jruby/RubyKernel.java:2054
Foo at extend_module_with_context.rb:21
(root) at extend_module_with_context.rb:16
#<Method: Class#the_method (defined in ModuleWithMethod at /home/mbj/devel/3928397/extend_module_with_context.rb:2)>
An exception occurred running extend_module_with_context.rb
undefined method `the_method_with_context' for Foo (NameError)
Backtrace:
 Kernel(Class)#method at kernel/common/kernel18.rb:163
Foo.__class_init__ (Foo) at extend_module_with_context.rb:21
Object#__script__ at extend_module_with_context.rb:16
 Rubinius::CodeLoader#load_script at kernel/delta/codeloader.rb:68
 Rubinius::CodeLoader.load_script at kernel/delta/codeloader.rb:110
 Rubinius::Loader#script at kernel/loader.rb:614
 Rubinius::Loader#main at kernel/loader.rb:815
#<Method: Class(ModuleWithMethod)#the_method>
extend_module_with_context.rb:21:in `method': undefined method `the_method_with_context' for class `Class' (NameError)
from extend_module_with_context.rb:21
#<Method: Class(ModuleWithMethod)#the_method>
extend_module_with_context.rb:21:in `method': undefined method `the_method_with_context' for class `Class' (NameError)
from extend_module_with_context.rb:21:in `<class:Foo>'
from extend_module_with_context.rb:16:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment