Project

lemo

0.0
No commit activity in last 3 years
No release in over 3 years
fast-ish Memoize that handles nils and singletons
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.10
>= 0
~> 10.0
>= 0
 Project Readme

Lemo Gem Version Build Status

Yet another memoize gem. Because it does some things that are important to me.

Attention paid to performance:

  • rewrites the memoized method for fast normal-path execution. Rewritten method only makes calls to ruby methods, apart from the necessary single call to the original method.

  • Uses instance variables for memoisation storage.

Lemo::Memo treats nil as a value, in other words uses presence or absence of an instance variable to establish whether the value has been memoised or not. Can clear one or several or all memoized values.

Lemo::Ormo behaves like @ivar ||= so clearing is just setting @ivar to nil. Or removing it.

_memoed_methods gives a hash of methods that have been memoised, and their unmemoised method bodies.

Works on singleton instances. Although the syntax is clunky and best avoided.

Is just as threadsafe as ||= (in other words it mostly isn't)

Will raise an exception if you attempt to memoise methods with parameters.

Installation

Add this line to your application's Gemfile:

gem 'lemo'

And then execute:

$ bundle

Or install it yourself as:

$ gem install lemo

Usage

require 'lemo/ormo'

class YourThing
  include Lemo::Ormo # or Lemo::Memo if you want nil-as-a-value and specialised clearing.

  def normal_method
    rand
  end

  memo def expensive_calculation
    # do complicated stuff that is referentially transparent
  end
end

your_thing = YourThing.new
your_thing.expensive_calculation
your_thing.clear_memos

Development

rspec to run specs

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/djellemah/lemo.

License

The gem is available as open source under the terms of the MIT License.