Calculator in Ruby for Beginners 15
Video Title: Calculator in Ruby for Beginners 15
Hello everyone welcome back today we’re going to be taking a look at creating a calculator in our Ruby beginner series as a reminder in previous videos we’ve covered creating additional files we’ve covered for Loops we’ve covered functions we’ve covered while loops and getting user input today we’re going to
Try and put some of this together let’s start by considering what a calculator should do so what I’m going to say is we want a calculator that can do the following first addition then subtraction multiplication division it should get user input and then it should print the result we also want to uh
Continually ask for input so a user a user can do multiple calculations per run let’s also say a user can quit by typing Q uh or Q like lowercase Q so these are just some things that I’d like to do what I’d like to start with is by just creating a basic calculator
And then we’ll do some refactoring where we gradually improve it to get started let’s say we want to have a main method I’m going to go ahead and call the main method I’m then going to create the main method by just saying def main inside of here what I want to do is
Say the uh user should start by getting a message that says welcome to the calculator just like this I’m going to get rid of all of this stuff it just printed because that feels like cheating I’m actually going to full screen this for now so what I’d like to do is allow
The user to choose add addition subtraction multiplication division and then quit the program so what I’d like to do now is say uh the user has a command right the they’ve issued a command the command is equal to gets. cho. downcase we’re going to be down
Casing this just in case the user sends us something like capital Q for quit and we just only want to check for a lowercase Q after we’ve done this we can then say all right if the command is q we should just puts goodbye the return keyword here allows
Us to break out of this main method and just return so that we don’t have to continue doing anything else this is almost like it’s guarding us from doing the rest of the program if the user puts in a Que Now the way this is going to
Work is we’re going to do this so that the user runs the calculator one time and then after we figure out what it’s like to run at one time we’ll then implement the feature that allows it to run a bunch of times times so we’ll just
Start with this it won’t make a lot of sense for how this will run a lot but hopefully if we write this well enough we’ll end up in a situation where we can then say all right let’s just Loop through this a bunch of times so now
That we’ve done this we need to say all right if the user command if the command was equal to the number one then we want to say they’ve chosen addition if the user’s command was equal to two we can say they’ve chosen subtraction we’re using elsif statements here three will be multiplication and
Four will be division puts I don’t understand that command is just if they put in like the word test because that’s really not an option here let’s go ahead and let’s take a look at what we should do here in all of these cases we probably need to make sure the user can
Just enter a new or a first number and a second number for this case right here let’s just go ahead and let’s return cuz we don’t understand what they want us to do so we shouldn’t even ask them for the numbers because it doesn’t really make
Sense so now let’s say puts what is your first number we can say first number is equal to gets. Chomp do2 and you have a choice here you can either use 2i for integer or 2f for float or decimal let’s use 2f we can then ask them for the
Second number and then we could say second number is the same thing now that we have both numbers and we know they want to use addition or subtraction or multiplication or division we’re good to go so let’s go ahead and let’s do this we’ll come down here and we’ll say if
The command is equal to one then we know it’s addition in that case we could start by doing a result is equal to first number plus second number we can then go ahead and print the result we can do an else if command equals 2 two and you can see this is
Actually very similar to what we have right here so I’m going to go ahead and copy this we’ll paste this in we don’t have this lse statement down here because this is a different block of code if the user chose subtraction we’re going to do pretty much the same thing
We had here but we need to change this to a subtraction if they chose multiplication we do the same thing and we’ll say this is a multiplication for division we’ll do the same thing but we’ll change this to a division symol now we have all of these done here
We’ve now asked the user for their input they’ve given us a proper input hopefully they’ve then given us a first number and a second number and then we’ve managed to print those results let’s go ahead and let’s try running this Ruby main. RB welcome to the calculator what would you like to do
I’ll start with addition you’ve chosen addition what is your first number five what is your second number five gives us 10 now let’s take a look at what we have here we have a command block that’s printing off a bunch of stuff for basically no reason we then
Get two numbers using pretty much the same logic and then we have a if statement again that mirrors our first if statement let’s think about how we can take this and break this up a bit now we have two options one is to break these up into individual functions and
The other one is is to Loop through all of the stuff let’s think about what looping through everything would look like if we Loop through everything we have to do a couple things multiple times we have to show the user welcome to the calculator we’re going to have to
Get the user’s command we’re going to have to check if the user wants to quit we’re then going to have to tell the user all right what’s your first number what’s your second number or I don’t understand and then we’re going to have to do the mathematic operation after we
Do that we then loop back back up here we start again and then we’re going to have to say here’s your options what’s your command so really what we could do here in our main function is we could just say while true do everything inside
Of here tab all of this over and then hit end down here and we basically have a working calculator sort of let’s try this we’ll say Ruby main. RB let’s type Q to quit and you can see goodbye let’s run it again let’s do addition first number is
Five second number is five we get 10.0 and then a welcome to the calculator so really what we need to do here is when we print this final result we should actually be using puts not print because we want a new line there it’s my fault for using AI instead of typing this
Myself let’s go ahead and let’s try this again let’s print addition first number five second number six is 11 we then welcome to the calculator again because we print that and then we go back up to the top to start over again we could actually take pretty much all of this
And just turn this into a calculator Loop let’s cut this and let’s call a calculator Loop and then let’s create another function here def calulator Loop and then in here we can just paste in our entire logic so we now have our calculator loop our main function which
Calls calculator Loop and then we start by calling the main function looks a lot cleaner like this but of course if we expand this we can see everything that’s in here let’s take this and let’s move this into a print welcome message we can then come up here and do
A def print welcome message and then we can just paste all of this in we also have the input that we’re getting getting here we have the user input and then we’re checking if they want to quit and some other stuff this is where we want to be
Careful about how we’re breaking these things up into various functions the issue is when we return here we’re returning or we’re breaking out of this while loop if we move all of this stuff into a separate function we would only be returning out of that function let’s take a look at what I
Mean I’m going to run the program by typing Ruby main. RB and I’m going to type Q to quit you can see here that gives us goodbye let’s take this logic right here and let’s move this let’s call this uh I don’t know get user
Input just like that we can then come up here do a def get user input and we can say command is equal to gets command if the command is equal to Q we want to puts goodbye and return if the command is equal to one 1 2 3 or whatever we
Then want to print this stuff out if you don’t understand we print don’t understand and then we return so this is what our git user input looks like now when we call G user input here if we run this we can see we get our welcome message I’ll type one
I’ll hit enter and it says you’ve chose addition what’s your first number you can say all right our first number is going to be five our second number is going to be six we can see here we get an undefined local variable or method command for main object why is that well
We get our Command in our user input function we check all of these things it prints addition so it looks like it’s working but when we get down here in our calculator Loop and we say if command is equal to one this command doesn’t exist anywhere in this calculator Loop it
Actually only exists in this git user input so once we’re done in this git user input we actually need to return this command if we try this method and now run this we can say one is addition we can type in our first number and our second number and this still won’t work
The reason is our Command is still undefined because we’re returning it but we actually need to also get it here so we need to say command is equal to get command from the user input if we now run this type one type five type six you
Can see it gives us 11 and then it runs the loop again so this is where you want to be a little bit careful just in case but okay this seems to be working for us I think we actually don’t even need this check here for uh putting all of
The addition subtraction multiplication or division operators what I want to do is say if the command does not equal one or the command does not equal 2 or the command does not equal 3 or the command does not equal 4 but notice if we have the or here what’s actually
Happening is we’re saying if the command does not equal one or two or 3 or four we’ll actually run into an issue when the command equals 2 for example the command does not equal one that this is true which means this will print before it even checks if the command does not
Equal two so we actually have to use the and operators here and not the or operators this is a bit of a trick that you can fall into where if you use the wrong operators you can you can enter this Loop without realizing it but you’ll also realize something else if we
Run this program now and we type one for addition you can see our first number is five our second number is six that prints 11 that works okay but what happens if our first or our Command is equal to six you can see I don’t understand that command what is your
First number why is it doing that well you see we’ve run into this problem where if our Command fails here we print that we don’t understand and then we just return nothing and instead of this breaking us out of the while loop like it did before it’s actually just
Returning nothing and then putting nothing into our Command here so what we actually have to do here is return something maybe like return turn zero and then we have to say if command was equal to zero then we have to go to the next iteration of this Loop the next
Keyword as a reminder takes you through all of this skips it and then starts the loop again it’s not really ideal to be returning Zero from something like this this isn’t an instance where we really just want to not have to do this type of logic this is also where you probably wouldn’t
Want to have these pieces of logic in the git user function this might make more sense as like a validate user input function so let’s do that let’s take this command move it out of here and now all we’re going to do is say if our
Command is equal to Q we’ll puts goodbye in return but we’ll deal with that in a second let’s now do a validate check so we’ll say uh if uh invalid command we’ll pass in the command if invalid command we will just print next then let’s come up here do a Def
And we’ll say invalid command this will take in a command end and then in here we’ll say if the command does not equal one and it does not equal Al two and it does not equal 3 and it does not equal 4 I don’t want to return that I don’t understand
The command what I actually want to do is return true I want to return true that these conditions have failed if these conditions have failed this will return true which means if next and then we can go to the next Loop and in here we can of course oops we can of course
Puts whatever error we want we could say puts I don’t understand that command and now let’s try running this Ruby main. RB I’ll type in the number six I don’t understand that command it then says that was invalid so we’ll go to the next iteration of our Loop
Cool but you’ll notice that there’s another piece of logic we don’t quite have and that’s our if command equals qit we can use the same type of logic here let’s say def uh check for quit check for quit this takes in a command and then in here we grab this
Bit right here cut it paste it in here and then instead of just returning we can just say return true and then down here we can say if check for quit command then we want to break we’re using next here because we want to do the loop again and we’re
Using break here because we want to break out of our while loop let’s go ahead and let’s try running this this if we try running this and we type Q you’ll see this doesn’t work but what’s actually happening here well if our Command does not equal 1 or
Two or 3 or four we don’t understand it so Q does not equal one and it doesn’t equal two and it doesn’t equal three and it doesn’t equal four so it tells us it doesn’t understand and then it goes to the next iteration of the loop before we
Check for quit so we also need to add something in here that says and the command oops the command does not equal q and now if we run this program again and we type Q you see goodbye and our program quits now this right here is
Kind of ugly because we have a bunch of different conditions inside of our if statement let’s try something else let’s take all of these and let’s say command equals uh or command equals number is equal to this conditional right here if the command or I guess we
Should say command does uh does not equal number if the command does not equal the number then we have this condition we also have the quit condition here so we could say command is invalid is equal to command does not equal number and command does not equal quit we can then use command
Is invalid here as our check if command is invalid puts I don’t understand that command return true this allows us to keep these things in our variables it’s still not the best way to do this but it is a beginner calculator so that cleans up our code a
Little bit we can then run this test it two for subtraction 6 and five gives us one we can type Q to quit all of that’s working we have our print message our Command we check for invalid check for quit let’s get our uh user numbers let’s say get user
Numbers this then comes up here we can do a def get user numbers get our numbers and then we need to return the first and the second number which means here we need to do first number comma second number equals get user numbers so now we have our
Numbers here let’s then do our math operations our math operations need to happen in a couple different ways let’s start by abstracting these a bit instead of doing first number plus second number let’s do add first number and second number we can do the same thing here for subtract we could do
Another one here for multiply and we can do another one here for divide that helps to clean up our code a little bit in case we want to change how these math operations work we do need to Define these so let’s come up here and let’s do
It before the calculator Loop let’s do a def add for first number and second number one for subtract one for multiply and then one for division you’ll see why we’re doing this in a second let’s go ahead and let’s try running this code run this code addition
First number is five second number is six that works let’s try division first number is 16 second number is four that works but now let’s try something else let’s run this program one more time choose division let’s divide 25 by 0 you can see here this gives us Infinity now
25 / 0 probably shouldn’t be Infinity what we want to do here is say that’s not really how math works right let’s move these prints into our actual math operations we can do that by instead of returning a result we can just print the result in here after we do
That we then have the ability to turn all of this into one line of code and then uh we actually don’t need to do this result either it gives us a much cleaner version of our if statement here we can then do the same thing we did before
Where we run all of these we get 11 that works but now in the case where our second number is zero we can actually say if second number is equal to zero puts you can’t divide by zero return end you can also do else and then put in this logic right
Here this allows us to do our check and then we should be good to go if we try running this we’ll do four for division we’ll type in 25 / by 0 you can’t divide by zero so now we have our error handling and you’ll notice that happens
Here even though we’re calling the function we can handle that logic ourselves by modifying how how the math Works we’re not necessarily changing the rules of addition or subtraction but we’re being a bit better about how we handle these errors right so that takes care of that let’s hit Q to exit let’s
Go ahead and let’s move this right here into its own function as well let’s do this as the uh calculate calculate method this takes in a first number a second number and a command we can come up here do a def calculate run this Loop and then be done here so now
Our main loop our calculator Loop is pretty much just this while loop that has a couple of steps in it it’s pretty easy to see what we’re doing here we’re saying print the welcome message get the user input check if they gave us an invalid command check if they want to
Quit get the first number and the second number from the user and then calculate that seems fine but our code is about 100 Lin lines in this file which does make it a little bit hard to read let’s go ahead and let’s refactor this a bit more let’s come into our file here
Create a new folder call this math inside of the math folder I want to create a new file I’m going to call this I don’t know operations. RB inside of operations I want to put in the divide multiply subtract and addition stuff I’ll take all of this I’ll put it into
Operations this seems a bit cleaner I can now come in here and at the top I’ll just say require relative math SL operations that still allows us to use this file it’s just moved into this folder first number five second number six that gives us 11 so that’s pretty cool what
Else can we move in there let’s go ahead and let’s do the input for the users create a new folder call this like input and then in here we’ll just say this is for for like uh user input. RB in here we’ll say anytime we want to get user
Input we’ll grab it from here so let’s move in our get user numbers uh and I think we have our uh git user input just like that and now we want to require relative our input as well we can do another one create a new folder call this validations in here
We’ll just say new file validate input or something and then in here we’ll do our invalid command just like that uh and I think that’s it right yeah it looks like it we’ll save that and then we need to require relative that validations validate input go ahead and
Remove all this empty space that we don’t need anymore and now our codee’s looking a little bit cleaner we we’re now down to 68 lines here with these things in multiple different files we have our Loop we have our calcul we have our check for
Quit at this point I would say you could probably go through and clean this up a bit more we run this we check for addition five and six that still works Q still works to quit I think it would make sense to move some of the stuff
Into another folder maybe we could have like a folder for our Loops like our main program folder or main logic or something I would say that would make a good exercise for the viewer you we’ now worked through a pretty involved program I would argue it’s more involved than it
Probably needs to be like this is just a simple calculator that we’ve probably over-engineered but I’d say let’s maybe try to move the calculate the check for quit and the prints into a different folder or a different file I’m sure you’ll be able to figure it out it’s
Going to be very similar to the other steps we’ve done here but this is a good way for you to check and more importantly hopefully to run into problems running into problems is where you do most of your learning it’s not from following along with people like me so
Hopefully as you try to move these things into a different file something goes wrong and then you have to fix it yourself because that’s where you’ll actually do your learning but for now thank you so much for watching and hopefully I will see you in the next one
Video Keywords: Ruby Programming, deanin,ruby for beginners,ruby tutorial for absolute beginners,coding tutorial in ruby,a simple guide for beginners,ruby for beginners tutorial,ruby tutorial for beginners,programming in ruby,age calculator in ruby,xml schema tutorial for beginners,ruby language for beginners,ruby projects for beginners,ruby exercises for beginners,getting started in ruby,learn to program in ruby,calculator,xml beginner tutorial,age calculator,programmin calculator
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
My last hope. Seeing as you use digitalocean, have you ever come across this problem with S3 style uploads with Spaces.
Create a blog post with an image added. Save it. Works fine. Show page shows the image.
If I go back to the edit page, the image link is broken. The image on the show page still works.
I am using DigitalOcean spaces ( like S3 ). The problem is that the file upload from ActionText is setting the representation of the image to public, but the original image blob to private.
I tried public: true on the storage.yml but that doesn't seem to change anything.
Any one, any ideas on this? Why are they different permissions?
It might be to do with this x-amz-acl=public-read header . I know that the problem is that the upload loads the original blob as private but representations as public. I can't seem to get around it other than making, recursively, all folders public.
@Deanin. Great work. Thank you