Skip to content
vic

txus/rubinius-memoize

Method memoization using Rubinius AST transforms

txus/rubinius-memoize.json
{
"createdAt": "2013-11-12T20:49:21Z",
"defaultBranch": "master",
"description": "Method memoization using Rubinius AST transforms",
"fullName": "txus/rubinius-memoize",
"homepage": "https://github.com/txus/rubinius-memoize",
"language": "Ruby",
"name": "rubinius-memoize",
"pushedAt": "2015-08-21T09:36:42Z",
"stargazersCount": 3,
"topics": [],
"updatedAt": "2021-07-16T06:16:55Z",
"url": "https://github.com/txus/rubinius-memoize"
}

Method memoization using [Rubinius][rubinius] AST transforms.

Add this line to your application’s Gemfile:

gem 'rubinius-memoize'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rubinius-memoize

For now it only supports methods with zero arguments.

Also, it only works on Rubinius 2.0+.

Warning if you don’t use Bundler (if you do you’re fine)

Section titled “Warning if you don’t use Bundler (if you do you’re fine)”

Since it extends the compiler at runtime, from the moment that you require 'rubinius/memoize', the memoize method will only be available in files required after that.

That means that this won’t work:

shawarma.rb
require 'rubinius/memoize'
class Shawarma
memoize def foo
3
end
end

But this will:

some_other_file.rb
require 'rubinius/memoize'
require 'shawarma'
class Shawarma
memoize def foo
expensive_operation
end
end
adonis = Shawarma.new
adonis.foo # takes a while
adonis.foo # instant!
# Want to expire the memoized method? Be my guest:
Rubinius::Memoize.expire(adonis, :foo)
adonis.foo # takes a while again

This was made by Josep M. Bach (Txus) under the MIT license. I’m [@txustice][twitter] on twitter (where you should probably follow me!).

[twitter] !: https://twitter.com/txustice [rubinius] !: http://rubini.us