Skip to content

Instantly share code, notes, and snippets.

@matthewd
Created May 27, 2010 15:19
Show Gist options
  • Select an option

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

Select an option

Save matthewd/415926 to your computer and use it in GitHub Desktop.
From 6b204921badcd210a063d7e87a73d186bc30ecc9 Mon Sep 17 00:00:00 2001
From: Matthew Draper <matthew@trebex.net>
Date: Fri, 28 May 2010 00:47:45 +0930
Subject: [PATCH] Remove redundant method lookup functions.
---
kernel/common/module.rb | 58 +++++++-------------------------------
kernel/common/variable_scope.rb | 2 +-
kernel/delta/module.rb | 2 +-
3 files changed, 13 insertions(+), 49 deletions(-)
diff --git a/kernel/common/module.rb b/kernel/common/module.rb
index 6399184..f5dbe9c 100644
--- a/kernel/common/module.rb
+++ b/kernel/common/module.rb
@@ -103,7 +103,7 @@ class Module
alias_method :inspect, :to_s
- def lookup_method(sym)
+ def lookup_method(sym, check_object_too=true)
mod = self
while mod
@@ -117,26 +117,8 @@ class Module
# Always also search Object (and everything included in Object).
# 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, check_object_too=true)
- mod = self
-
- while mod
- if entry = mod.method_table.lookup(sym.to_sym)
- return entry
- end
-
- mod = mod.direct_superclass
- end
-
- # 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)
+ return Object.lookup_method(sym)
end
end
@@ -180,24 +162,6 @@ class Module
return out
end
- def find_class_method_in_hierarchy(sym)
- Rubinius.object_metaclass(self).find_method_in_hierarchy(sym)
- end
-
- def remote_alias(new_name, mod, current_name)
- entry = mod.find_method_in_hierarchy(current_name)
- unless entry
- raise NameError, "Unable to find method '#{current_name}' under #{mod}"
- end
-
- meth = entry.method
-
- @method_table.store new_name, entry.method, entry.visibility
- Rubinius::VM.reset_method_cache(new_name)
-
- return new_name
- end
-
def undef_method(*names)
names.each do |name|
name = Type.coerce_to_symbol(name)
@@ -245,26 +209,26 @@ class Module
def public_method_defined?(sym)
sym = Type.coerce_to_symbol(sym)
- m = find_method_in_hierarchy(sym, false)
- m ? m.public? : false
+ mod, meth = lookup_method(sym, false)
+ meth ? meth.public? : false
end
def private_method_defined?(sym)
sym = Type.coerce_to_symbol(sym)
- m = find_method_in_hierarchy(sym, false)
- m ? m.private? : false
+ mod, meth = lookup_method(sym, false)
+ meth ? meth.private? : false
end
def protected_method_defined?(sym)
sym = Type.coerce_to_symbol(sym)
- m = find_method_in_hierarchy(sym, false)
- m ? m.protected? : false
+ mod, meth = lookup_method(sym, false)
+ meth ? meth.protected? : false
end
def method_defined?(sym)
sym = Type.coerce_to_symbol(sym)
- m = find_method_in_hierarchy(sym, false)
- m ? m.public? || m.protected? : false
+ mod, meth = lookup_method(sym, false)
+ meth ? meth.public? || meth.protected? : false
end
##
@@ -424,7 +388,7 @@ class Module
if entry = @method_table.lookup(name)
entry.visibility = vis
- elsif find_method_in_hierarchy(name)
+ elsif lookup_method(name)
@method_table.store name, nil, vis
else
raise NoMethodError, "Unknown #{where}method '#{name}' to make #{vis.to_s} (#{self})"
diff --git a/kernel/common/variable_scope.rb b/kernel/common/variable_scope.rb
index 1cf16fd..ffbf602 100644
--- a/kernel/common/variable_scope.rb
+++ b/kernel/common/variable_scope.rb
@@ -80,7 +80,7 @@ module Rubinius
if sup = @module.direct_superclass
# This will probably break for define_method
- return sup.find_method_in_hierarchy(@method.name) != nil
+ return sup.lookup_method(@method.name) != nil
end
return false
diff --git a/kernel/delta/module.rb b/kernel/delta/module.rb
index 18ed932..32bc715 100644
--- a/kernel/delta/module.rb
+++ b/kernel/delta/module.rb
@@ -39,7 +39,7 @@ class Module
mc = Rubinius.object_metaclass(self)
args.each do |meth|
method_name = Type.coerce_to_symbol meth
- method = find_method_in_hierarchy(method_name)
+ mod, method = lookup_method(method_name)
mc.method_table.store method_name, method.method, :public
set_visibility method_name, :private
end
--
1.7.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment