capecoder
capecoder

Reputation: 23

Issues with method scope

I am continually receiving the following error when running this program:

taxed_output.rb:30:in `initialize': undefined local variable or method `input1' for #<Receipt:0x007fd8f511b428> (NameError) from taxed_output.rb:81:in `new' from taxed_output.rb:81:in `<main>'

I understand that this probably has something to do with variable scope, and I am new to Ruby. Is there any way that I can adjust my code to allow input1 through input3 to be seen by the new Receipt instance? (I've also played around with creating it as a singleton, but am not sure I did it right... I renamed the variables Receipt.var_name)

Thanks in advance!

CODE:

class Receipt

input1 = [ "1 imported box of chocolates at 10.00", "1 imported bottle of perfume at 47.50" ]
input2 = [ "1 book at 12.49", "1 music CD at 14.99", "1 chocolate bar at 0.85" ]
input3 = [ "1 imported bottle of perfume at 27.99", "1 bottle of perfume at 18.99", "1 packet of headache pills at 9.75", "1 box of imported chocolates at 11.25" ]

non_tax = [ "chocolate", "chocolates", "book", "pills" ]

import_tax = 0.05
sales_tax = 0.10
round = 20.0

def div
    "-" * 10
end #div

def initialize
    puts div + "OUTPUT 1" + div
    get_input(input1)
    puts div + "OUTPUT 2" + div
    get_input(input2)
    puts div + "OUTPUT 3" + div
    get_input(input3)
end #initialize

# GET_INPUT: splits input arrays and saves values into individual variables
def get_input (input_arr)
    total_tax = 0
    total_price = 0
    input_arr.each do |item|
       #items
           split_item_array = item.split
           qty = split_item_array[0].to_i
           price = split_item_array[-1].to_f
           p = item.split(" at ")
           product_name = p[0].delete("/0-9/").strip
       #taxes
           tax = tax_cal(price, product_name)
           total_tax += tax
           taxed_price = (price.to_f + tax)
           total_price += taxed_price
        puts "#{qty} #{product_name}: #{tax_price.round(2)}"
    end #each
    puts "Sales Tax: #{total_tax}.round(2)"
    puts "Total: #{total_price}.round(2)"
end #def (get_input)

# TAX_CAL: assesses applicable taxes based on product characteristics
def tax_cal (price, product)
    tax_exclude = []
    a_product = product_name.split(" ")
    tax_exclude = a_product & non_tax
    import_sales_tax = import_tax + sales_tax

    if product.include?(‘imported’) and tax_exclude.count != 1
      tax = price.to_f * import_sales_tax
    elsif product.include?(‘imported’) and tax_exclude.count == 1
      tax = price.to_f * import_tax
    elsif tax_exclude.count != 1
      tax = price.to_f * sales_tax
    else
      tax = 0
    end #if

    return tax
end #def (tax_cal)

end #class

Receipt.new

Upvotes: 0

Views: 26

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52357

You can do one of the following:

  • make input1, input2, input3 constants;
  • make input1, input2, input3 methods;
  • make input1, input2, input3 class variables;
  • make input1, input2, input3 class instance variables.

(the same applies to non_tax, import_tax,sales_tax, round etc..)

I would go with either of the first two options.

Choosing second option will cost you less code changes.

Upvotes: 1

Related Questions