Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

don't define class_eval on Kernel

we don't need to do this
just switch behaviour based on type
  • Loading branch information...
commit 26f21ad87fccfc4a0d55926a620694fdc2a80ea5 1 parent ffbec4f
@matthewrudy authored
Showing with 10 additions and 7 deletions.
  1. +9 −1 lib/memoist.rb
  2. +1 −6 lib/memoist/core_ext/singleton_class.rb
View
10 lib/memoist.rb
@@ -22,6 +22,14 @@ def self.escape_punctuation(string)
string.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')
end
+ def self.memoist_eval(klass, *args, &block)
+ if klass.respond_to?(:class_eval)
@dkubb
dkubb added a note

You could even cut out a bit of duplication with this:

def self.memoist_eval(klass, *args, &block)
  klass = klass.singleton_class unless klass.respond_to?(:class_eval)
  klass.class_eval(*args, &block)
end
@matthewrudy Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ klass.class_eval(*args, &block)
+ else
+ klass.singleton_class.class_eval(*args, &block)
+ end
+ end
+
module InstanceMethods
def self.included(base)
base.class_eval do
@@ -91,7 +99,7 @@ def memoize(*method_names)
unmemoized_method = Memoist.unmemoized_method_for(method_name, identifier)
memoized_ivar = Memoist.memoized_ivar_for(method_name, identifier)
- class_eval do
+ Memoist.memoist_eval(self) do
include InstanceMethods
if method_defined?(unmemoized_method)
View
7 lib/memoist/core_ext/singleton_class.rb
@@ -5,9 +5,4 @@ class << self
self
end
end unless respond_to?(:singleton_class) # exists in 1.9.2
-
- # class_eval on an object acts like singleton_class.class_eval.
- def class_eval(*args, &block)
- singleton_class.class_eval(*args, &block)
- end
-end
+end

1 comment on commit 26f21ad

@pboling

Very cool.

Please sign in to comment.
Something went wrong with that request. Please try again.