Skip to content

Commit

Permalink
don't define class_eval on Kernel
Browse files Browse the repository at this point in the history
we don't need to do this
just switch behaviour based on type
  • Loading branch information
matthewrudy committed Mar 10, 2013
1 parent ffbec4f commit 26f21ad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
10 changes: 9 additions & 1 deletion lib/memoist.rb
Expand Up @@ -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)

This comment has been minimized.

Copy link
@dkubb

dkubb Mar 12, 2013

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

This comment has been minimized.

Copy link
@matthewrudy

matthewrudy via email Mar 13, 2013

Author Owner
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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions lib/memoist/core_ext/singleton_class.rb
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool.

Please sign in to comment.