Bank class withdraw


My witdraw method in Account class isnt functioning properly.. In particular im talkin about the case statement. when it equals 0

module Errors
def no_money
@e = "You have no money"
@ee = "You are below 50 please deposit some money!"

class Account
include Errors
attr_reader :name, :balance
def initialize(name,balance = 100)
@name = name
@balance = balance

def display_balance(pin_number)
puts pin_number == pin ? "Balance: $#{@balance}" : pin_error


def withdraw(pin_number, amount)
@balance -= amount
puts "Withdrew #{amount}. New balance: #{@balance}. "
puts "#{no_money[@ee]}" if @balance < 50 #The brackets in the brackets where it says no_monet thats why they neccessary because you got 2 instance variables in no_money method so it targets.
case @balance
when 0
puts "#{no_money[@e]}" #This isn' working findout why.

		if pin_number != pin
		puts pin_error

def deposit(pin_number,amount)
if pin_number == pin
@balance += amount
puts " Deposited amount: #{amount} New balance: #{@balance}. "
puts pin_error

#Include error checking that prevents users from overdrawing their accounts

def pin
@pin = 1234

def pin_error
"Access denied: incorrect PIN."

checking_account ="Al")

Replace this line with your code.


Please include some kind of query in your post. Right now you've said that there is a problem, but not what it is, why you're unable to address it, what you want to know.. It's really hard to give an answer to that. It also helps to make sure that your code's formatting is intact.


I have stated that the case statement within the withdraw method of the class isn't working. Meaning that when my balance hits 0 the case statement doesn't activate. I am wondering why it's not working meaning the case statement.


The word "working" is relative to what you want. I would have to guess. That creates the chance of guessing wrong and spending time on something other than you want.

Making it easy to reply gets you a faster better response at lower effort from the person replying. Not making it easy to reply shows that you don't value other people's time and they'll be less likely to spend any time at all.

If you have a glass jar that is difficult to open and you want help with it. Would you bring it to the person you are asking for help? Or would you have them run up 5 flights of stairs to where you live first? Which one gets you the jar opened faster? Which one will be more likely to succeed in getting help?

If the 0 case doesn't match then perhaps the value wasn't 0. Print it out to to investigate. You can also print out the result of testing if it's equal to 0 so that you know you're not missing anything subtle.


I am sorry but i don't get what your trying to say. I don't know how i didn't make it clear by saying my case statement isn't working. I assumed you would understand what i meant just by looking at the code itself. I specifically pointed out what the problem was by saying its the case statement in the withdraw method.


You explaining it in ten words is faster and more accurate than reading code and guessing how it in your opinion misbehaves.

You posted over 50 lines of code which is badly formatted. Reading it is not convenient easy or pleasant.

Saving yourself from describing the problem also shows how you value your own time compared to that of others.

There's a pretty good article on this here:
The glass jar analogy really should say enough to convince you on this matter. Just the same if you want to improve on this and understand my perspective better than that article may be of value to you.


Ok im sorry about the confusion, is there anyway you Can you teach me how to format it to other's? Because iv'e gotten this complaint before and my code is organized usually and all im doing is copy and pasting it to the post.

And in regarding my problem, let me try...... So the bank class Account has a method withdraw where i can keep taking money out the more i call the withdraw method. The case statement within the method is there to trigger when my instance variable @balance hits 0. But in this program it doesn't. I created the module which is suppose to be accessed when the balance hits 0.


There are formatting tools in the text editor, click the one for preformatted text after having highlighted the code

My suggestion is still to investigate whether it really did hit 0
If it did hit 0, then the when 0 part probably executed and your observation that it didn't is wrong.
If it didn't hit 0, then... whatever makes you say that you did hit 0, wasn't happening


It does land on 0 so i don't know what the case statement wont trigger.


Then it probably does trigger. Whatever is making you say it isn't triggering, is probably not entirely correct. Finding out if it triggers is as easy as adding a print inside the case that says "hello" or something


Bro, i tested it out and runned the code on the command promp and the case statement is activating. IF it did then it would access the module and print the @ee instance variable.


Problem solved then. It activates and it prints the variable. (Or you are contradicting yourself)

If it does activate but doesn't print the intance variable, then the code that is printing the instance variable isn't right - you just determined that it runs, but that it has no effect. So that's what's wrong.


Lol so why does it have no effect?


Why would it?


puts "#{no_money[@e]}"

The expression evaluated is


What does that do?


no_money is a method. It's going to return something.
[] is also a method, it too will do something. Which method it is depends on what no_money returns.
@e is an instance variable, you're using that as the argument to the []-method of the value that no_money returns.

If you look at no_money you can see that it returns a string.
So the []-method is therefore String#[] which you can read about here:
@e is a string, so the argument given to String#[] is also a string. String#[] has some behaviour for that (which you can read about in its documentation)


But the @@e instance variable worked and ran fine in the command prompt the @ee instance variable didn't run!


Yes they both work. They do something. But what does work mean? (You are using the word work to avoid describing what it should be doing)

What does the method return?
What is the value of @ee?

When you take that value your method returns, and use the []-method on it, with the value of @ee, what does that all evaluate to?

"blah blah blah"["h bl"]
=> "h bl"

I also think that when you wrote that you didn't think through why or what it would be doing. You need to be able to motivate why something will have a particular result.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.