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 8263743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:12:32+00:00 2026-06-08T04:12:32+00:00

Edited to include SSCE I have a Proc object which I add to a

  • 0

Edited to include SSCE

I have a Proc object which I add to a Hash with other Procs:

ten_percent_discount_over_sixty = Proc.new {
  cart.each { |item| cart_total += item.price }

  if cart_total >= 60.00
    cart_total =- cart_total * 0.1
  end
}

As you can see there is a cart array which contains items. I can’t however get my cart instance variable into the scope of this proc. So later when I iterate through all of these rules in another method, I get undefined variable errors.

The point of doing this is that I have various promotional rules (functions) that need to be run on this cart object. It’s possible there’s a better way to store rules that can be applied iteratively to a objects instance variable.


I have checkout object which contains a cart (an array) of item objects; items have the attributes: code, price and name.

Next I have a Promotion object which holds a hash of promotional rules. This hash contains Proc objects that hold these rules (which are functions that each run on the cart object changing the final price of the cart). Rules maybe added and removed by calling a method and with the name as an argument.

The problem I am having is when I iterate through the hash of promotional rules. I don’t know how to get the cart object into the scope of those Proc objects in order to run the functions contained in those Proc objects, changing the cart state.

  1. Do I pass in the cart object when creating the Proc/lambda?
  2. Create a cart instance variable in the Promotion object and set/get it?
  3. Somehow pass in the cart variable within the checkout object?
  4. I’m over-thinking or approaching the problem in an incorrect manner?

The full code is quite long so I’ll post a gist below:

https://gist.github.com/3163127

  • 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-06-08T04:12:34+00:00Added an answer on June 8, 2026 at 4:12 am

    Your rule procs must accept cart as an argument, act on it, and return modified cart.

    cart = {total: 100}
    
    rules = {ten_percent_discount: lambda{|c| c[:total] *= 0.9; c },
      five_percent_discount: lambda{|c| c[:total] *= 0.95; c}}
    
    rules.each do |name, rule|
      cart = rule.call cart.dup
    end
    
    cart # => {:total=>85.5}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What gives the best performance? (edited to include another option) var myVec:Vector.<Number> = new
I have a jsfiddle and have edited it to include sessions for passing values
I have edited the wordpress functions file to include the function add_before_content . This
// edited by Neil Butterworth to conserve vertical space #include <stdio.h> struct A; struct
I have a (DB2) database table containing location information, one column of which is
I want to create a textbox, which can be edited and shows the text
(Edited to try to explain better) We have an agent, written in C++ for
Update: Edited code example to use AutoA for the workaround (which was the original
I have an existing database structure created outside or Rails. I've edited one of
var categoryModel = AfvClassifiedsDB.Categories.Include(Listings) .Single(c => c.Title == categoryName); var viewModel = new ClassifiedsBrowseViewModel

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.