Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3239470
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:58:26+00:00 2026-05-17T17:58:26+00:00

Trying to follow along with a metaprogramming screencast from pragpub and ran into some

  • 0

Trying to follow along with a metaprogramming screencast from pragpub and ran into some problems because of changes in Ruby since the release of screencast.

Hard to explain the problem w/o the code, so here’s that:

class Discounter
  def discount(*skus)
    expensive_discount_calculation(*skus)
  end

  private

  def expensive_discount_calculation(*skus)
    puts "Expensive calculation for #{skus.inspect}"
    skus.inject {|m, n| m + n }
  end
end

def memoize(obj, method)
  ghost = class << obj; self; end
  ghost.class_eval do
    define_method(method) do |*args|
      memory ||= {}
      memory.has_key?(args) ? memory[args] : memory[args] = super(*args)
    end
  end
end

d = Discounter.new
memoize(d, :discount)

puts d.discount(1,2,3)
puts d.discount(1,2,3)
puts d.discount(2,3,4)
puts d.discount(2,3,4)

Problem: The local variable in the method memorize should only change (by taking the return value from Discounter#discount) if it is being passed different arguments than it previously was.

For example I expect the output from running the code above to look like:

Expensive calculation for [1, 2, 3]
6
6
Expensive calculation for [2, 3, 4]
9
9

But this is the actual output:

Expensive calculation for [1, 2, 3]
6
Expensive calculation for [1, 2, 3]
6
Expensive calculation for [2, 3, 4]
9
Expensive calculation for [2, 3, 4]
9

Why isn’t the local variable persisting across the calls? What am I missing to make this code work?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:58:26+00:00Added an answer on May 17, 2026 at 5:58 pm

    If you define a local variable inside a block, it will vanish when the end of the block is reached.

    To achieve the lifetime you want, you need to define the memory variable before the block:

    def memoize(obj, method)
      memory = {}
      ghost = class << obj; self; end
      ghost.class_eval do
        define_method(method) do |*args|
          memory.has_key?(args) ? memory[args] : memory[args] = super(*args)
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to follow the steps to compile some C code to import into
I'm trying to follow along with some tutorials on the web on Xcode programming.
I'm trying to follow along with Railscasts #235 (Omniauth in a rails app). Having
I am using Xcode 4.3.2 and trying to follow along with the Learn Objective-C
I'm trying to follow along with Paul Sheriff's e-book Fundamentals of N-tier which is
I'm trying to follow along this tutorial to enable remote access to MySQL. The
I'm trying to follow along with http://mongotips.com/b/array-keys-allow-for-modeling-simplicity/ I have a Story document and a
I'm trying to follow along with http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options In an effort to do ./node_modules/.bin/mocha --reporter
I'm trying to follow along the demo source code for the Draggable jQuery method,
I'm trying to follow along with a tutorial. In the tutorial, the instructor shows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.