Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ruby 1.9 compatible

* *_methods method returns methods as symbols in 1.9 whereas 1.8
  was strings. Just have to check for both sym and string version
  • Loading branch information...
commit f47ad4c68b33240b6fee113d7e701b945132f3b1 1 parent 31ff046
Joel Friedman authored
Showing with 15 additions and 7 deletions.
  1. +9 −3 lib/simple_memoize.rb
  2. +6 −4 test/test_simple_memoize.rb
12 lib/simple_memoize.rb
View
@@ -14,7 +14,7 @@ def memoize(*method_names)
memoized_method_name = "#{stripped_method_name}_with_memo#{punctuation}"
regular_method_name = "#{stripped_method_name}_without_memo#{punctuation}"
- unless (instance_methods + private_instance_methods).include?(method_name)
+ unless __include_method_name__( (instance_methods + private_instance_methods), method_name )
raise NoMethodError, "The Method '#{method_name}' cannot be memoized because it doesn't exist in #{self}"
end
return if self.method_defined?(memoized_method_name)
@@ -31,11 +31,17 @@ def #{memoized_method_name}(*args)
alias_method :#{regular_method_name}, :#{method_name}
alias_method :#{method_name}, :#{memoized_method_name}
- protected :#{method_name} if protected_instance_methods.include?('#{regular_method_name}')
- private :#{method_name} if private_instance_methods.include?('#{regular_method_name}')
+ protected :#{method_name} if __include_method_name__( protected_instance_methods, '#{regular_method_name}' )
+ private :#{method_name} if __include_method_name__( private_instance_methods, '#{regular_method_name}' )
"
end
end
+
+ private
+ # Necessary because ruby1.9 now returns methods as symbols not strings
+ def __include_method_name__(methods, method_name)
+ methods.include?( method_name ) || methods.include?( method_name.to_sym )
+ end
end
end
10 test/test_simple_memoize.rb
View
@@ -120,14 +120,16 @@ def test_class_method_calls_method_several_times
def test_protected_methods_remain_protected
dog = Dog.new
- assert dog.protected_methods.include?('protected_growl_without_memo')
- assert dog.protected_methods.include?('protected_growl')
+ methods = dog.protected_methods
+ assert methods.include?('protected_growl_without_memo') || methods.include?( :protected_growl_without_memo )
+ assert methods.include?('protected_growl') || methods.include?( :protected_growl )
end
def test_private_methods_remain_private
dog = Dog.new
- assert dog.private_methods.include?('private_growl_without_memo')
- assert dog.private_methods.include?('private_growl')
+ methods = dog.private_methods
+ assert methods.include?('private_growl_without_memo') || methods.include?( :private_growl_without_memo )
+ assert methods.include?('private_growl') || methods.include?( :private_growl )
end
def test_cant_memoize_a_missing_method
Please sign in to comment.
Something went wrong with that request. Please try again.