

Ask HN: Help me refactor this code please (Ruby) - grover_hartmann

It&#x27;s basically a vending machine simulation, I want to refactor it into two classes, a model (VendingMachine) and a Controller. I&#x27;m not sure what else to do. Please help.<p>https:&#x2F;&#x2F;gist.github.com&#x2F;anonymous&#x2F;a978dcd30383f3e618e5
======
grover_hartmann
So I would like to rename VendingMachine to Controller and have a
VendingMachine model where I move #vend and #refund to it.

I'm also not very happy with #menu being this large in LoC. What would you
guys recommend?

~~~
BlackjackCF
Having a vending machine model where it interacts with the balance and price
is a good call.

I would also think about renaming either your refund method or your refund
variable. It gets a little confusing.

You should also think about the fact that your refund method's case statement
doesn't have a default. That means if someone doesn't put in either "y" or
"n", you're screwed. Same situation with menu. You should always code for the
unhappy path, meaning that you should think through for when someone decides
to screw with your program.

In your menu case statement, I don't understand why you can't just do this:

case input when 1 name = "Coffee" price = @items[:coffee] ... #end of case
statement

vend(name, price).

That'll DRY up your code at least a little bit.

~~~
grover_hartmann
Something like this perhaps?

[https://gist.github.com/fbdf89c0117c3e1452cf](https://gist.github.com/fbdf89c0117c3e1452cf)

~~~
grover_hartmann
Or maybe this?

[https://gist.github.com/anonymous/41c829f53777c4cdd41c](https://gist.github.com/anonymous/41c829f53777c4cdd41c)

------
technion
Think about types. You call .to_i on the balance, so it can only initially be
an integer. But you have items costing 1.50.

