Banking with Ruby: Introduction to Methods: Part 1

Lewis Coldwell
The Yorkshire Coder
6 min readFeb 23, 2018

In this post I will start to introduce methods through a practical example. The end product will be a functioning banking app that allows the user to deposit, withdraw, bring up a transaction history and check the balance.

This app will be written in ruby so its best to start off by loading up our favourite editor: VScode. Once you have this open create a file in a new directory thats easy to find. You should now open up your Terminal and navigate to this newly created folder. Now lets get started!

If we want to build a simple app that just shows a balance; it is true we could just type a small block of code like this:

puts "Welcome to the banking app! Do you want to see your balance? Y or N"
response = gets.chomp
if response == "Y" then
puts "Your Balance is $100"
elsif response == "N" then
puts "Thanks for using the app"
else
puts "Invalid Selection, try again"
end

From my previous posts we have already gone through ‘if’ statements — so this is pretty simple stuff. However it doesn’t give us much wiggle room to add in multiple options and requests from the end user. In addition, if we were to write this out multiple times we would end up with an incredible amount of code and repetition. Therefore we need to define methods. Let’s do that now:

def bal
balance = 0
puts "your balance is $#{balance}"
end

Then lets link that to our original code:

puts "Welcome to the banking app! Do you want to see your balance? Y or N"
response = gets.chomp
if response == "Y" then
bal
elsif response == "N" then
puts "Thanks for using the app"
else
puts "Invalid Selection, try again"
end

Great! Response ‘Y’ will now link to our defined method. This is all still very basic, so lets jazz it up! We start by creating a variable for balance (it currently is set as a constant: 0) so, when we add our deposit and withdrawal features later we can update the one variable and reflect our new balance. To do this, we should create an array that will allow the program to access and add up or subtract our values later. At the top of your page add the following:

balance = array.new

And now we should amend all instances of where we call the method ‘bal’ to ‘bal(balance)’. For example:

def bal(balance)
puts "your balance is $#{balance.sum}"
puts welcome(balance)
end

Notice we have omitted the value for balance in this method because it’s now defined at the top of our code. Another important aspect I’ve introduced here is “calling” the local variable. This is where I added (balance) to the method ‘def bal’. This is because if we don’t call in a local variable into the method it doesn’t know that it exists when we try to use it within the method. As our app gets more complicated we will begin to add more of these calls to increase the methods “scope”. Also note that i put ‘puts welcome(balance)’ at the end. This is part of my plan to make the app loop. Just put that in for now and in the next step I will explain how this relates to the initial menu.

In our next step, we should redesign our menu and start to build a loop that allows the user to continue making requests until they choose to exit. So let’s put our landing message in a method and introduce a case statement to give the opportunity to add options for the end user. We will build on this menu later once we’ve defined the methods to carry out the rest of our transactions:

def welcome(balance)puts "Welcome to the banking app, please enter 'b' for balance or 'e' for exit:"response = gets.chomp.downcase      case response
when "b"
bal(balance)
when "e"
exit
else
clear
error
welcome(balance)
end
endputs welcome(balance)

So there are three new methods we need to define ‘exit’, ‘error’ and ‘clear’. These will come become useful later in our code to keep it nice and DRY (http://bit.ly/1hQ65ME). I’m sure you can define these yourselves but to give you an example of what I did see below. In addition, I’ve placed a ‘puts’ message at the bottom to initiate our starting method ‘welcome(balance).

def exit
puts "Thank you for banking with us!"
end
def error
puts "Error! Invalid selection, please try again."
end
def clear
system("clear")
end

Run your app in Terminal and check its all working out at this stage and we will go into the nitty gritty of defining another local variable and adding in new functions!

Before we go any further lets introduce out next array variable: history! We are going to want to store our transactions in an array to so we can bring up our account history. At the top of the page, just like we did for balance, lets create a new history array:

history = Array.new

For safe measure, just like we did for balance, ensure each method that has instances of this local variable is called. Otherwise the method will have no scope to access the value. To recap, for ‘def welcome(balance)’ we will rename it ‘def welcome(balance, history)’. Indeed, under the ‘else’ condition I mention ‘welcome(balance) so you’d need to rename that. You get the idea. If we have a method that requires that variable it must be in scope and called at the start of the method. You will find errors along the way otherwise (In a later post, I will cover some common errors and how to debug them). Lets now introduce a transaction history method to the program, firstly add an option in our menu (note how i’ve now called my new history variable, you will need to go back and make sure your previous methods are updated to reflect any changes to the method name!):

def his(balance, history)
puts "This is your history"
puts history
puts bal(balance, history)
end
Your new menu at this stage

Once you have this menu and the history method set up — we can now begin to start building transactions. Introduce a deposit method that pushes the deposit amount to the balance array. Then when we call the balance it will add the sum of the values we enter and give us our balance. Lets see how this method will look:

def dep(balance, history)
puts "How much would you like to deposit"
input = ' '
input = gets.chomp.to_i
puts "you are depositing$#{input}"
balance.push input
history << input
puts bal(balance, history)
end

Don’t forget to add an option in your menu for this new method!! Give the program a run and if you encounter any errors, study them and locate the line. With this kind of method the bug is normally in the scope. Make sure you are labelling your methods correctly!

Now it’s time to add our withdrawal function. This can be a bit more complicated than our previous methodology because we need to convert our input value into a negative integer. This is easy to do but requires a small amount of patience first. Building this method starts off simple:

def withd (balance, history)
puts "How much would you like to withdraw?"
input = ' '
input = gets.comp.to_i

So this all looks familiar right now, but what if the user withdraws more than what their account balance contains? We need to specify an argument that disallows such behaviour. Continuing the above function, lets add:

if balance.sum < input
puts error
else
puts "you have chosen to withdraw $#{input}"

Now the integer we have received is a positive number, so in the next functions we will push to the same arrays we have done so before, but need to make sure its subtracting from the balance. In addition, we need to make sure it reflects a negative amount in the transaction history. Add this last part to the method:

balance.push -input
history.push -input
puts bal(balance, history)
end
end

Give that a run, and now you have a fully functional banking app! What we need to do now is store our users data so it remembers who we are when we run the app again. It could also do with some security. In part 2 of the the methods series we will expand on this app.

This is how your end product should look

--

--

Lewis Coldwell
The Yorkshire Coder

Currently studying Full Stack Development at the Coder Academy, Sydney in their bootcamp program.