# Homework Help - Methods

**URL:** <https://www.ruby-forum.com/t/homework-help-methods/178283>\
**Category:** Ruby\
**Created:** [November 2, 2009, 4:56am UTC](https://www.ruby-forum.com/t/homework-help-methods/178283 "2009-11-02T04:56:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rickbocas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rickbocas](https://www.ruby-forum.com/u/rickbocas)\
**Post date:** [November 2, 2009, 4:56am UTC](https://www.ruby-forum.com/t/homework-help-methods/178283/1 "2009-11-02T04:56:35Z")

</div>

The goal of this assignment is to ask the user for the price of each of  
their items and print out a receipt using your own defined methods. I’ve  
been doing well in the class(ahead actually) until the last two weeks  
and I want to stay ahead.

This is what I have so far…

puts “Enter shopping cart item prices, one per line.\n”  
puts “Press 0 when you are finished.\n”

items\_in\_cart = []  
subtotal\_cart = 0.0  
subtotal\_tax = 0.0  
total\_cost = 0.0  
SALES\_TAX = 0.07

def get\_items\_from\_user()  
items = []  
item = ask\_user\_for\_number(“Enter item price:”)

while (item != 0.0) do  
items.push(item)

```
item = ask_user_for_number("Enter item price:")

```

end

return items  
end

def ask\_user\_for\_number(question\_to\_ask)  
puts question\_to\_ask  
response = gets().chomp().to\_f()  
end

def get\_subtotal(cart\_items)  
subtotal = 0.0  
cart\_items.each() do |item|  
subtotal = subtotal + item  
end

return subtotal  
end

def calculate\_tax (cart\_total)  
return cart\_total \* SALES\_TAX  
end

puts get\_items\_from\_user  
puts ask\_user\_for\_number  
puts get\_subtotal  
puts calculate\_tax

puts "Item subtotal: " + subtotal\_cart.to\_s()  
puts "Sales Tax: " + subtotal\_tax.to\_s()  
puts "Total: " + total\_cost.to\_s()

I keep getting this error

rb:43 in ‘ask\_user\_for\_number’ : wrong number of arguments (0 for 1)  
(Argument Error)

I have no idea what this means…and I don’t see what’s wrong in the  
‘ask\_user\_for\_number’ method so, I don’t even know where to begin to fix  
it.

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![rickbocas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rickbocas](https://www.ruby-forum.com/u/rickbocas)\
**Post date:** [November 2, 2009, 5:20am UTC](https://www.ruby-forum.com/t/homework-help-methods/178283/2 "2009-11-02T05:20:56Z")

</div>

On 2009-11-02, Rick B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def ask\_user\_for\_number(question\_to\_ask)  
> puts question\_to\_ask  
> response = gets().chomp().to\_f()  
> end

You defined “ask\_user\_for\_number” as a method taking an argument.

> puts ask\_user\_for\_number

Here, you call ask\_user\_for\_number with no argument. Thus, it has  
the wrong number of arguments. (0 for 1) means you gave it zero and  
it needed 1.

> I have no idea what this means…and I don’t see what’s wrong in the  
> ‘ask\_user\_for\_number’ method so, I don’t even know where to begin to fix  
> it.

Nothing’s wrong with it. What’s wrong is the call to it down by the  
puts later.

-s

---

<div class="post-metadata">

**Author:** ![rickbocas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rickbocas](https://www.ruby-forum.com/u/rickbocas)\
**Post date:** [November 2, 2009, 6:03am UTC](https://www.ruby-forum.com/t/homework-help-methods/178283/3 "2009-11-02T06:03:41Z")

</div>

Rick B. wrote:

> The goal of this assignment is to ask the user for the price of each of  
> their items and print out a receipt using your own defined methods. I’ve  
> been doing well in the class(ahead actually) until the last two weeks  
> and I want to stay ahead.
> 
> This is what I have so far…
> 
> puts “Enter shopping cart item prices, one per line.\n”  
> puts “Press 0 when you are finished.\n”

…and why are those lines there instead of in the method  
get\_items\_from\_user() (which is a misnomer anyway because the method is  
getting item prices not items, e.g. item = bread, item\_price=$2.50)?

How about get\_prices() and get\_input() instead of get\_items\_from\_user()  
and ask\_user\_for\_number()?

---

<div class="post-metadata">

**Author:** ![rickbocas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rickbocas](https://www.ruby-forum.com/u/rickbocas)\
**Post date:** [November 2, 2009, 6:55am UTC](https://www.ruby-forum.com/t/homework-help-methods/178283/4 "2009-11-02T06:55:57Z")

</div>

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?

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…  
How do I get the array “prices” into the get\_subtotal method?  
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…

---

<div class="post-metadata">

**Author:** ![rickbocas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rickbocas](https://www.ruby-forum.com/u/rickbocas)\
**Post date:** [November 2, 2009, 10:29am UTC](https://www.ruby-forum.com/t/homework-help-methods/178283/5 "2009-11-02T10:29:15Z")

</div>

Rick B. 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…

---

<div class="post-metadata">

**Author:** ![rickbocas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rickbocas](https://www.ruby-forum.com/u/rickbocas)\
**Post date:** [November 2, 2009, 10:57am UTC](https://www.ruby-forum.com/t/homework-help-methods/178283/6 "2009-11-02T10:57:11Z")

</div>

7stud – wrote:

> 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)

One other thing, a method call in your code is replaced by the return  
value of the method. So the call to my\_meth in the code above will get  
replaced by the return value of my\_meth. So when you execute your  
program, the line:

user\_prices = my\_meth

will become:

user\_prices = [2.5, 1.24, 6.80]

If a method doesn’t have a return statement, then the value of the last  
expression that was executed will be returned. In the show() method,  
the last expression that’s executed is the puts statement, and puts  
returns the value nil. You can alter the code to see that:

return\_val = show(user\_prices)  
puts return\_val

–output:–  
nil

However, since the original example just says:

show(user\_prices)

and a method call is replaced by its return value, and because show()  
does not have a return statement and therefore the value of the last  
expression is returned, and the last expression that is executed is  
puts, the method call is replaced in the code with nil. So the line:

show(user\_prices)

becomes:

nil

That may look strange having nil sitting on a line by itself, but you  
can write a program like this:

puts “starting program”  
10  
nil  
“hello”  
puts “ending program”

–output:–  
starting program  
ending program

Values sitting on lines by themselves are simply discarded. That  
program is equivalent to:

def meth1  
return 10  
end

def meth2  
return nil  
end

def meth3  
return ‘hello’  
end

puts “starting program”  
meth1  
meth2  
meth3  
puts “ending program”

If you call a method and don’t store the return value in a variable, the  
return value is discarded. However, instead of this:

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 #\<----\*\*\*\*\*this here  
show(user\_prices)

You could simply write:

show(my\_meth)

The method call my\_meth gets replaced in the code by its return value  
which is the array [2.5, 1.24, 6.80]. Therefore, the line:

show(my\_meth)

becomes:

show([2.5, 1.24, 6.80])

However, its clearer if you use two statements:

user\_prices = my\_meth  
show(user\_prices)

…and clarity trumps brevity every time.
