alexwayfer / alt_memery Goto Github PK
View Code? Open in Web Editor NEWThis project forked from tycooon/memery
A gem for memoization in Ruby.
License: MIT License
This project forked from tycooon/memery
A gem for memoization in Ruby.
License: MIT License
gem "alt_memery", "~> 2.1", require: "memery"
This gem has a different name for the primary entry point than bundler expects. Two options are to add an alt_memery.rb
which loads memery
, or update README with instruction like above.
Is all of this expected, intentional? Just want to manage my expectations when using the library.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'alt_memery', require: false
end
require "memery"
class A
include Memery
memoize def call(time = Time.now)
puts "called at #{time}"
42
end
end
a = A.new
puts "Call 1 (use default)"
a.call
puts "Call 1 (use default) Done\n\n"
sleep(1)
puts "Call 2 (use default)"
a.call
puts "Call 2 (use default) Done\n\n"
sleep(1)
puts "Call 3 (Time.now)"
a.call(Time.now)
puts "Call 3 (Time.now) Done\n\n"
sleep(1)
puts "Call 4 (Time.now 2nd)"
a.call(Time.now)
puts "Call 4 (Time.now 2nd) Done\n\n"
sleep(1)
puts "Call 5 (Time.new(2002))"
a.call(Time.new(2002, 10, 31, 2, 2, 2, "+02:00"))
puts "Call 5 (Time.new(2002)) Done\n\n"
sleep(1)
puts "Call 6 (Time.new(2002) 2nd)"
a.call(Time.new(2002, 10, 31, 2, 2, 2, "+02:00"))
puts "Call 6 (Time.new(2002) 2nd) Done\n\n"
And here's the output:
Call 1 (use default)
called at 2024-01-12 17:23:48 +0700
Call 1 (use default) Done
Call 2 (use default)
Call 2 (use default) Done
Call 3 (Time.now)
called at 2024-01-12 17:23:50 +0700
Call 3 (Time.now) Done
Call 4 (Time.now 2nd)
called at 2024-01-12 17:23:51 +0700
Call 4 (Time.now 2nd) Done
Call 5 (Time.new(2002))
called at 2002-10-31 02:02:02 +0200
Call 5 (Time.new(2002)) Done
Call 6 (Time.new(2002) 2nd)
Call 6 (Time.new(2002) 2nd) Done
To summarize...
The following calls w/ arguments are memoized, such that when called again they are not re-executed:
Time.now
, which normally in Ruby is evaluated fresh on every call, but with memoization it is not. This is the same behavior as some other memoization libraries, and maybe it has to be this way...a.call
a.call(Time.new(2002, 10, 31, 2, 2, 2, "+02:00"))
The following calls w/ arguments are not memoized, such that when called again they are freshly executed each time called:
a.call(Time.now)
Tested on Ruby 3.2.2
ref: https://stackoverflow.com/questions/53783291/ruby-default-arguments-static-or-dynamic
Clear caches only for specific methods.
After #93 I assumed I understood how it would behave when memoizing methods with parameters that are complex objects (like the Time objects in that example). My hope that that it would follow the same "identity" comparison that Rails uses to know that two ActiveRecord objects are the same, but that is not the case, as the methods are called repeatedly with the same object being passed every time.
Going to poke around in the source code for a while to look for an easy way to hack a solution.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.