Coder Social home page Coder Social logo

alt_memery's People

Contributors

alexwayfer avatar dependabot-preview[bot] avatar depfu[bot] avatar jelf avatar martinstreicher avatar nevinera avatar tycooon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

pboling

alt_memery's Issues

Setup instructions

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.

How are arguments supposed to be handled?

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:

  1. Default argument value is 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
  1. When the Time argument is representing the same time as a previous call, the memoized result is used. This is cool.
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

Memoize methods with ActiveRecord objects as parameters

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.