Rick Barrett wrote:
> def get_input(question_to_ask)
>   puts question_to_ask
>   input = gets().chomp().to_f()
> end
> 
> def get_price()
>   prices = []
>   price = get_input("Enter item price: ")
> 
>   while (price != 0.0) do
>     prices.push(price)
>     price = get_input("Enter item price: ")
>   end
> 
>   return prices
> end
> 
> So...basically get_input is inside of get_price...so there's no need to 
> call it down later in the code? Right?
>

Right.

> Then there's the second half of the code...
> 
> def get_subtotal(prices)
>   subtotal = 0.0
> 
>   prices.each do |price|
>     subtotal = subtotal + price
>   end
> 
>   return subtotal
> end
> 
> def get_tax(tax)
>   return tax * SALES_TAX
> end
> 
> get_price
> get_subtotal
> get_tax
> 
> puts "Subtotal: $" + subtotal.to_s()
> puts "Sales Tax: $" + tax.to_s()
> puts "Total: $" + total.to_s()
> 
> I get the same error (0 for 1) (Argument Error) for get_subtotal...
>

Once again, look at your method definition.  How many parameter 
variables are there in the get_subtotal definition?  How many values did 
you call the method with?

If you define a method like this:

def my_meth(a, b, c, d)
    #do something
end

Then you MUST call my_meth with 4 arguments--not 3 arguments, and not 0 
arguments.  So you can never write:

my_meth

You have to call my_meth like this:

my_meth(10, 20, 5, 6)

See how there are 4 things between the parentheses in the method call? 
Now look above and see how my_meth was defined with 4 variable names 
between the parentheses?  The values in the method call:

my_meth(10, 20, 5, 6)

namely 10, 20, 5, and 6 are called the "arguments".  The variable names 
in the method definition:

def my_meth(a, b, c, d)

namely a, b, c, and d are called the "parameter variables" or 
"parameters".  Guess what?  When you call my_meth, ruby lines up the 
method call with the method definition like this:


    my_meth(10, 20, 5, 6)
def my_meth( a,  b, c, d)

Then ruby assigns the values on top(the arguments) to the variables 
underneath them(the parameter variables), like this:

a = 10
b = 20
c = 5
d = 6

Then inside the method if you write something like:

result = a * d
puts result

60 will be displayed.


> How do I get the array "prices" into the get_subtotal method?

Look at this example:

def my_meth
  prices = [2.5, 1.24, 6.80]
  return prices
end

def show(arr)
  str = arr.join(", ")
  puts str
end


user_prices = my_meth
show(user_prices)


> How do I get the input from the get_prices method to be added together 
> in the get_subtotal method?
> And then how do I get it to print each of those if the variables stay 
> inside the methods?
> 
> I've searched the internet and read the method chapters we're on in my 
> book and I can't find the answer...or an explanation that I can 
> understand...for some reason I just can't get this...

-- 
Posted via http://www.ruby-forum.com/.