Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change implementation from prepend to UnboundMethod #1

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ B.call # => 42
```

## Difference with other gems
Memery is very similar to [Memoist](https://github.com/matthewrudy/memoist). The difference is that it doesn't override methods, instead it uses Ruby 2 `Module.prepend` feature. This approach is cleaner and it allows subclasses' methods to work properly: if you redefine a memoized method in a subclass, it's not memoized by default, but you can memoize it normally (without using awkward `identifier: ` argument) and it will just work:
Memery is very similar to [Memoist](https://github.com/matthewrudy/memoist). The difference is that it doesn't create additional prefixed methods, instead it uses `UnboundMethod` as local variable. This approach is cleaner and it allows subclasses' methods to work properly: if you redefine a memoized method in a subclass, it's not memoized by default, but you can memoize it normally (without using awkward `identifier: ` argument) and it will just work:

```ruby
class A
Expand Down
40 changes: 12 additions & 28 deletions lib/memery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,25 @@ def self.method_visibility(klass, method_name)

module ClassMethods
def memoize(method_name)
prepend_memery_module!
define_memoized_method!(method_name)
end

private

def prepend_memery_module!
return if defined?(@_memery_module)
@_memery_module = Module.new
prepend @_memery_module
end

def define_memoized_method!(method_name)
mod_id = @_memery_module.object_id
visibility = Memery.method_visibility(self, method_name)
raise ArgumentError, "Method #{method_name} is not defined on #{self}" unless visibility
old_method = instance_method(method_name)

@_memery_module.module_eval do
define_method(method_name) do |*args, &block|
return super(*args, &block) if block
define_method(method_name) do |*args, &block|
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be conviment way to obtain source of original method in pry or irb

Memery allows it with $ method_name -s in pry or method(:method_name).super_method.source in irb which is good

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be conviment way to obtain source of original method in pry or irb

Then there should be specs for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that viewing the source code of memoized method from pry/irb is very important, but OK, it can be an argument.

Copy link
Owner

Choose a reason for hiding this comment

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

Unfortunately, $ method_name -s doesn't work in Pry, but I hope that some day it will get fixed.
A.instance_method(:x).super_method.source technique does work, though, so we might want to just add that spec. I don't see much harm in having some extra ancestors.

Copy link
Contributor Author

@AlexWayfer AlexWayfer Apr 19, 2018

Choose a reason for hiding this comment

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

irb(main):002:0> def foo
irb(main):003:1> p :bar
irb(main):004:1> end
=> :foo
irb(main):005:0> method(:foo).source
NoMethodError: undefined method `source' for #<Method: main.foo>
	from (irb):5
	from /home/alex/.rbenv/versions/2.4.4/bin/irb:11:in `<main>'

What are you talking about?

Copy link
Owner

Choose a reason for hiding this comment

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

A.instance_method(:x).super_method.source works everywhere in case you required method_source gem.

But that's just an example, the idea is, with current implementation it is possible to get the actual method's source code using Ruby, and with this implementation it becomes impossible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. This MR is just offer, proof of concept, showcase of alternative way. If you think that getting source from Ruby (pry) is more important than a lot of Memery in ancestors in case of inheritance (and maybe some other unpleasant consequences, about which I don't remember or don't know) — you can just close this MR. And in the future, if some obvious shortcomings of the prepend approach will be revealed — you and others will know about another not bad (in my opinion) approach, and apply it, or create a fork with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, $ method_name -s doesn't work in Pry, but I hope that some day it will get fixed.

Just a ref to issue: pry/pry#1756

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With this implementation you can show the source of original method by moving (stepping) into method from memery and calling $ original_method from method body.

Copy link
Owner

Choose a reason for hiding this comment

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

Maybe, but you can't do method(:x).super_method.source as stated in Readme.

return old_method.bind(self).call(*args, &block) if block

@_memery_memoized_values ||= {}
@_memery_memoized_values ||= {}

key = [method_name, mod_id].join("_").to_sym
store = @_memery_memoized_values[key] ||= {}
key = :"#{method_name}_#{old_method.object_id}"
store = @_memery_memoized_values[key] ||= {}

if store.key?(args)
store[args]
else
store[args] = super(*args)
end
if store.key?(args)
store[args]
else
store[args] = old_method.bind(self).call(*args)
end

send(visibility, method_name)
end

send(visibility, method_name)
end
end

Expand Down
4 changes: 3 additions & 1 deletion spec/memery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ class << self
end

specify do
expect { klass }.to raise_error(ArgumentError, /Method foo is not defined/)
expect { klass }.to raise_error(
NameError, /undefined method `foo' for class `#<Class:0x\w+>'/
)
end
end
end