matthewrudy / memoist

ActiveSupport::Memoizable with a few enhancements
MIT License
920 stars 99 forks source link

Calling `memoize` on methods whose names are certain symbols causes syntax errors #89

Open patbl opened 3 years ago

patbl commented 3 years ago

For example:

require 'memoist'

class Foo
  extend Memoist

  def [](bar)
    rand
  end
  memoize :[]
end
[22:03:27] ~/projects  ➜ ruby brackets.rb
Traceback (most recent call last):
        8: from brackets.rb:3:in `<main>'
        7: from brackets.rb:9:in `<class:Foo>'
        6: from /Users/pat/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/memoist-0.16.2/lib/memoist.rb:127:in `memoize'
        5: from /Users/pat/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/memoist-0.16.2/lib/memoist.rb:127:in `each'
        4: from /Users/pat/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/memoist-0.16.2/lib/memoist.rb:131:in `block in memoize'
        3: from /Users/pat/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/memoist-0.16.2/lib/memoist.rb:55:in `memoist_eval'
        2: from /Users/pat/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/memoist-0.16.2/lib/memoist.rb:55:in `class_eval'
        1: from /Users/pat/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/memoist-0.16.2/lib/memoist.rb:205:in `block (2 levels) in memoize'
/Users/pat/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/memoist-0.16.2/lib/memoist.rb:205:in `module_eval': /Users/pat/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/memoist-0.16.2/lib/memoist.rb:213: syntax error, unexpected '(', expecting `end' (SyntaxError)
...        value = _unmemoized_[](*args)
...                              ^

This is true also of methods such as +, +@, -, -@, &, |, etc.

pboling commented 4 months ago

FYI: Added this alert to the new memoist repo

[!IMPORTANT]

Recommendation

Consider using MemoWise instead, as it is maintained, fully tested, provides thread safety guarantees, and is much, much faster.

Other Alternatives

In case you need a tool with this feature set that is currently maintained, check out:

[!TIP]
Seriously though, read the important note above.

[!WARNING]
If you must continue - be aware that this is unmaintained software.