• Stars
    star
    919
  • Rank 49,718 (Top 1.0 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 13 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ActiveSupport::Memoizable with a few enhancements

Memoist

Build Status

Memoist is an extraction of ActiveSupport::Memoizable.

Since June 2011 ActiveSupport::Memoizable has been deprecated. But I love it, and so I plan to keep it alive.

Usage

Just extend with the Memoist module

require 'memoist'
class Person
  extend Memoist

  def social_security
    puts "execute!"
    decrypt_social_security
  end
  memoize :social_security
end

person = Person.new

person.social_security
# execute!
# => (returns decrypt_social_security)

person.social_security
# => (returns the memoized value)

And person.social_security will only be calculated once.

Every memoized function (which initially was not accepting any arguments) has a (reload) argument you can pass in to bypass and reset the memoization:

def some_method
  Time.now
end
memoize :some_method

Calling some_method will be memoized, but calling some_method(true) will rememoize each time.

You can even memoize method that takes arguments.

class Person
  def taxes_due(income)
    income * 0.40
  end
  memoize :taxes_due
end

This will only be calculated once per value of income.

You can also memoize class methods.

class Person

  class << self
    extend Memoist
    def with_overdue_taxes
      # ...
    end
    memoize :with_overdue_taxes
  end

end

When a sub-class overrides one of its parent's methods and you need to memoize both. Then you can use the :identifier parameter in order to help Memoist distinguish between the two.

class Clock
  extend Memoist
  def now
     "The time now is #{Time.now.hour} o'clock and #{Time.now.min} minutes"
  end
  memoize :now
end

class AccurateClock < Clock
  extend Memoist
  def now
    "#{super} and #{Time.now.sec} seconds"
  end
  memoize :now, :identifier => :accurate_clock
end

Reload

Each memoized function comes with a way to flush the existing value.

person.social_security       # returns the memoized value
person.social_security(true) # bypasses the memoized value and rememoizes it

This also works with a memoized method with arguments

person.taxes_due(100_000)       # returns the memoized value
person.taxes_due(100_000, true) # bypasses the memoized value and rememoizes it

If you want to flush the entire memoization cache for an object

person.flush_cache   # returns an array of flushed memoized methods, e.g. ["social_security", "some_method"]

Authors

Everyone who contributed to it in the rails repository.

  • Joshua Peek
  • Tarmo Tänav
  • Jeremy Kemper
  • Eugene Pimenov
  • Xavier Noria
  • Niels Ganser
  • Carl Lerche & Yehuda Katz
  • jeem
  • Jay Pignata
  • Damien Mathieu
  • José Valim
  • Matthew Rudy Jacobs

Contributing

  1. Fork it ( https://github.com/matthewrudy/memoist/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Released under the MIT License, just as Ruby on Rails is.

More Repositories

1

rudeq

A rails DB-based queue plugin
Ruby
44
star
2

fake-code4hk-app

Decompiling a fake code4hk / occupy central app distributed via whatsapp
32
star
3

memoist2

Simple Memoization for Ruby 2.0
Ruby
27
star
4

rcov_rails

Ruby, Rails, Rcov put together into a neat Rake task
Ruby
19
star
5

regexpert

A collection of common Regexps for Ruby. Validation for emails, uk postcode, etc.
Ruby
16
star
6

solr_query

a ruby library designed to make building nested Solr queries simple and standardized.
Ruby
9
star
7

the-rude-book-of-elixir

Who knows?
9
star
8

acts_as_configurable

Avoid cluttering your Rails models, ActsAsConfigurable abstracts all your configuration away with a polymorphic config store
Ruby
9
star
9

cmon

C'mon, lets make monitoring simpler
Ruby
8
star
10

laowaihua

a "Lorem Ipsum" generator for Chinese
Ruby
6
star
11

super_state

super simple state machine for activerecord
Ruby
6
star
12

expiring_memory_store

Extend Rails, ActiveSupport caching to support an :expires_in option.
Ruby
5
star
13

aspicious

Super-simple implementation of Aspects for Ruby
Ruby
5
star
14

rudebay

an ebay watching twitter bot written in ruby
Ruby
4
star
15

matthewrudy.com

MatthewRudy.com is the home of Matthew Rudy Jacobs (me)
CSS
4
star
16

has_one_accessor

Access attributes on HasOne Associations as though they were on the base class
Ruby
3
star
17

make_specs_better

the perfect plugin to make your Rspec tests run like a dream.
Ruby
3
star
18

serve-this

EXPERIMENTAL - serve this directory over rack
Ruby
3
star
19

groupy

Categorise and Group Records
Ruby
3
star
20

rude-blog

A hastily written blogging application - apparently Tumblr isn't available in China.
Ruby
3
star
21

stubble

(experimental) reversible stubbing
Ruby
3
star
22

twissandra-rb

Twissandra rebuilt in Ruby
Ruby
3
star
23

classy_tee

Class Table Inheritance for ActiveRecord and Rails
Ruby
2
star
24

whitelist-mail-proxy

A thin proxy for Mail and ActionMailer to enable whitelisting
Ruby
2
star
25

hello-android-app

going through "Hello Android"
Java
2
star
26

make_tests_better

A rails plugin to make your Test Unit tests much much better automagically
Ruby
2
star
27

defenders_of_the_erb

Defend your ERb in Rails. Protect your database from too many uncached database calls in your views!
Ruby
2
star
28

rude-bench

Benchmarks
Ruby
2
star
29

time_freeze

Simple gem for freezing Time
Ruby
2
star
30

percentise

simple, consistent percentages
Ruby
2
star
31

matai

testing repository and gems with unicode names
1
star
32

coffee-talk

My talk on Coffee at HKROR
JavaScript
1
star
33

grpc-cache

playing with GRPC with a cache example
Go
1
star
34

Whether

Pretty weather for Hong Kong
Ruby
1
star
35

showoff-pootonghua

Showoff Talk on PooTongHua
Ruby
1
star
36

kubernetes-play

Playing with Kubernetes and Google Container Engine for Codeaholics June 2015
Go
1
star
37

perforation

Ruby
1
star
38

lrug_talk

Ruby
1
star
39

mr_freeze

object.freeze! freezes all sub-objects in an Array or Hash
Ruby
1
star
40

voteable_benchmarks

Compare performance of SQL v.s MongoDB implementations on up / down voting problem
Ruby
1
star
41

git-rebase-test

playing around with git rebase
1
star
42

wordpuzzle

playing with solving a word puzzle
Scala
1
star
43

cnapper

A tutorial project for building an offline-processed screenshot-ing application
Ruby
1
star
44

old-newsprank

JavaScript
1
star
45

repose

Manage your Repos! Just relax.
Ruby
1
star
46

bigrepo

200meg of files, bunged into a tiny repository
1
star
47

spine-talk

A talk on Spine.js for Codeaholics.hk on 29th June 2011
JavaScript
1
star
48

analdroid-talk

a short talk on using Google Analytics with Android
JavaScript
1
star
49

i18n_scope

simple, chainable, scoping for i18n
Ruby
1
star
50

sammichang.com

SammiChang.com
1
star
51

ruby-vs-scala

A selection of code in Ruby and Scala for speed and beauty
Scala
1
star
52

pinny

Simple Pinyin Translation
Ruby
1
star
53

showoff-laowaihua

Showoff Talk on LaoWaiHua
JavaScript
1
star
54

Elixir-RomanNumerals

the roman numerals kata performed in elixir
Elixir
1
star
55

red-dot-talk

my talk at red dot ruby conf
JavaScript
1
star
56

HKRoR

The Hong Kong Ruby On Rails Website
JavaScript
1
star
57

afriworks

an attempt to create a portal for outsourcing companies in ghana
Ruby
1
star