Skip to content

Instantly share code, notes, and snippets.

@matthewd
Created May 25, 2010 14:12
Show Gist options
  • Select an option

  • Save matthewd/413163 to your computer and use it in GitHub Desktop.

Select an option

Save matthewd/413163 to your computer and use it in GitHub Desktop.
diff --git a/kernel/common/module.rb b/kernel/common/module.rb
index fe15498..6399184 100644
--- a/kernel/common/module.rb
+++ b/kernel/common/module.rb
@@ -116,13 +116,13 @@ class Module
end
# Always also search Object (and everything included in Object).
- # This lets a module alias methods on Kernel.
- if instance_of?(Module) and self != Kernel
+ # This lets a module alias methods on Object or Kernel.
+ if instance_of?(Module)
return Object.lookup_method(sym)
end
end
- def find_method_in_hierarchy(sym)
+ def find_method_in_hierarchy(sym, check_object_too=true)
mod = self
while mod
@@ -133,9 +133,9 @@ class Module
mod = mod.direct_superclass
end
- # Always also search Object (and everything included in Object).
- # This lets a module alias methods on Kernel.
- if instance_of?(Module) and self != Kernel
+ # Optionally also search Object (and everything included in Object);
+ # this lets a module alias methods on Object or Kernel.
+ if check_object_too and instance_of?(Module)
return Object.find_method_in_hierarchy(sym)
end
end
@@ -244,26 +244,26 @@ class Module
end
def public_method_defined?(sym)
- sym = Type.coerce_to_symbol sym
- m = find_method_in_hierarchy sym
+ sym = Type.coerce_to_symbol(sym)
+ m = find_method_in_hierarchy(sym, false)
m ? m.public? : false
end
def private_method_defined?(sym)
- sym = Type.coerce_to_symbol sym
- m = find_method_in_hierarchy sym
+ sym = Type.coerce_to_symbol(sym)
+ m = find_method_in_hierarchy(sym, false)
m ? m.private? : false
end
def protected_method_defined?(sym)
- sym = Type.coerce_to_symbol sym
- m = find_method_in_hierarchy sym
+ sym = Type.coerce_to_symbol(sym)
+ m = find_method_in_hierarchy(sym, false)
m ? m.protected? : false
end
def method_defined?(sym)
sym = Type.coerce_to_symbol(sym)
- m = find_method_in_hierarchy sym
+ m = find_method_in_hierarchy(sym, false)
m ? m.public? || m.protected? : false
end
diff --git a/kernel/delta/module.rb b/kernel/delta/module.rb
index 2f47550..18ed932 100644
--- a/kernel/delta/module.rb
+++ b/kernel/delta/module.rb
@@ -16,7 +16,7 @@ class Module
Rubinius::VM.reset_method_cache(new_name)
else
- if ai = __metaclass_object__
+ if kind_of?(Class) && ai = __metaclass_object__
raise NameError, "Unable to find '#{current_name}' for object #{ai.inspect}"
else
thing = kind_of?(Class) ? "class" : "module"
diff --git a/spec/ruby/core/module/alias_method_spec.rb b/spec/ruby/core/module/alias_method_spec.rb
index 182b8f9..44f9698 100644
--- a/spec/ruby/core/module/alias_method_spec.rb
+++ b/spec/ruby/core/module/alias_method_spec.rb
@@ -55,5 +55,21 @@ describe "Module#alias_method" do
ModuleSpecs::ReopeningModule.foo.should == true
lambda { ModuleSpecs::ReopeningModule.foo2 }.should_not raise_error(NoMethodError)
end
-
+
+ it "can access a method defined on Object from Kernel" do
+ Object.class_eval do
+ def method_on_object_class
+ end
+ end
+
+ Kernel.module_eval do
+ alias :alias_on_kernel_module :method_on_object_class
+ end
+
+ Kernel.public_instance_methods.map {|m| m.to_s }.include?('method_on_object_class').should == false
+ Object.public_instance_methods.map {|m| m.to_s }.include?('method_on_object_class').should == true
+
+ Kernel.public_instance_methods.map {|m| m.to_s }.include?('alias_on_kernel_module').should == true
+ Object.public_instance_methods.map {|m| m.to_s }.include?('alias_on_kernel_module').should == true
+ end
end
diff --git a/spec/ruby/core/module/method_defined_spec.rb b/spec/ruby/core/module/method_defined_spec.rb
index 4ecb867..4089987 100644
--- a/spec/ruby/core/module/method_defined_spec.rb
+++ b/spec/ruby/core/module/method_defined_spec.rb
@@ -23,6 +23,12 @@ describe "Module#method_defined?" do
ModuleSpecs::Child.method_defined?(:private_super_module).should == false
end
+ it "returns false when called on a module and given a method defined on Kernel" do
+ m = Module.new
+
+ m.method_defined?(:require).should == false
+ end
+
it "raises a TypeError when the given object is not a string/symbol/fixnum" do
c = Class.new
o = mock('123')
diff --git a/spec/ruby/core/module/public_spec.rb b/spec/ruby/core/module/public_spec.rb
index 7841d18..3b1d0cb 100644
--- a/spec/ruby/core/module/public_spec.rb
+++ b/spec/ruby/core/module/public_spec.rb
@@ -5,4 +5,27 @@ describe "Module#public" do
it "on a superclass method calls the redefined method" do
ModuleSpecs::ChildPrivateMethodMadePublic.new.private_method_redefined.should == :after_redefinition
end
+
+ it "makes a private Object method public" do
+ m = Module.new do
+ public :require
+ end
+
+ m.public_instance_methods.map {|m| m.to_s }.include?('require').should == true
+ end
+
+ it "can access a method defined on Object from Kernel" do
+ Object.class_eval do
+ def private_method_on_object_class
+ end
+ private :private_method_on_object_class
+ end
+
+ Kernel.module_eval do
+ public :private_method_on_object_class
+ end
+
+ Kernel.public_instance_methods.map {|m| m.to_s }.include?('private_method_on_object_class').should == true
+ Object.public_instance_methods.map {|m| m.to_s }.include?('private_method_on_object_class').should == false
+ end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment