Baby’s First Ruby App
So I've begun to learn Ruby (for work), and I thought I might share a little bit of my progress so far. Progress might be an exaggeration considering how basic this application actually is, but at least I'm slowly but surely moving in the right direction anyway.
So here's the 1st part of it:
1: #GET INPUT
2: puts "Please enter a number"
3: STDOUT.flush
4: num1 = gets.chomp.to_i
5:
6: puts "Please enter another number"
7: STDOUT.flush
8: num2 = gets.chomp.to_i
9:
10: puts "Please pick the type of operation:
11: (A)dd
12: (S)ubtract
13: (M)ultiply
14: (D)ivide)"
15: STDOUT.flush
16: oper = gets.chomp
So here we're asking the user for 2 numbers, cleaning up the input, and then storing it into 2 variables (num1 and num2). We then ask the user to select which type of mathematical operation they'd like to perform (addition, subtraction, multiplication, or division).
The 2nd part:
1: #SELECT THE OPERATION
2: if oper == ("a" or "A")
3: add(num1, num2)
4: elsif oper == ("s" or "S")
5: subtract(num1, num2)
6: elsif oper == ("m" or "M")
7: multiply(num1, num2)
8: elsif oper == ("d" or "D")
9: divide(num1, num2)
10: end
So here we're deciding what we're going to do with our 2 variables (num1 and num2) based on the users selection. If they enter "a" or "A" (for example) we will then execute the method called "add" on line 3. Speaking of these methods...
The 3rd and final part:
1: #DEFINE THE METHODS
2: def add(num1, num2)
3: sum = num1 + num2
4: puts "#{num1} + #{num2} = #{sum}"
5: end
6:
7: def subtract(num1, num2)
8: if num1 > num2
9: diff = num1 - num2
10: puts "#{num1} - #{num2} = #{diff}"
11: else
12: diff = num2 - num1
13: puts "#{num2} - #{num1} = #{diff}"
14: end
15: end
16:
17: def multiply(num1, num2)
18: prod = num1 * num2
19: puts "#{num1} X #{num2} = #{prod}"
20: end
21:
22: def divide(num1, num2)
23: if num1 > num2
24: div = num1 / num2
25: puts "#{num1} / #{num2} = #{div}"
26: else
27: div = num2/num1
28: puts "#{num2} / #{num1} = #{div}"
29: end
30: end
Each one of these methods (starting on lines 2, 7, 17, and 22) perform a particular set of functions based on the variables that are being passed in (num1 and num2). These methods are called based on the earlier code (part 2) that selected which method applied based on which of the mathematical operations the user selected. The 2 numbers that the user entered are passed into that method, some type of calculation is carried out, and then the result is displayed to the user. In the case of subtraction and division, I elected to add an additional step that would determine which of the 2 entered numbers was larger, and then it would order the calculation accordingly so as to not return a negative number (but this is purely optional).
To recap, and provide a plain English flow of how this happen in this simple little application: the user enters 2 numbers, and then they select a mathematical operation. Based on that operation we go through a series of checks to match which operation was selected, the 2 numbers entered by the users are passed to the method, and then we enter the applicable method. The method then performs the necessary calculations and displays the output to the user.
Pretty simple, not exactly rocket science, but it's a start at least. I wanted to play with conditional statements a bit, as well as creating and calling my own methods. Next up will be to continue to experiment with arrays and hashes.
Going to Brooklyn, and the past 2 weeks
I'm taking a break from packing up for my trip to Brooklyn this weekend (to see HER), so I thought I'd briefly summarize the past few weeks of my life. I'm sure I'll forget to include a few things, but here's the short list of everything I'd completed (or things that remain to be completed) recently:
- History and systems -- 6 page thoughtful response to The Metaphysical Club
- History and systems -- 15-20 page research paper discussing the Mind/Body Problem (dualism, materialism, idealism)
- Philosophy -- paper discussing the validity of knowledge, and what we can truly know for certain
- Philosophy -- paper discussing the types (or type, depending on your opinion) of substances in the world.
- Cognitive psychology -- 5 page paper discussing how I would improve the human memory system.
- Cognitive psychology -- test on decision making, creativity, artificial intelligence, general intelligence, and problem solving.
- Math -- investment project examining the effects of initial investment, length of investment, and interest rate as individual factors that effect the final amount of money.
- Math -- 12 page paper briefly discussing the historical foundations of cryptography and it's modern versions (on a very basic level, I didn't get into all the really ugly details).
- Math -- present a brief poster and presentation on what I learned about cryptography. We're talking 2-3 minute presentation; not a big deal.
- Psychology of personality -- 5 page paper relating Alfred Adler to events in my own life. We had to pick a personality theorist that we felt we could relate with.
- Psychology of personality -- test on existential psychology, humanistic psychology, Gestalt psychology, and object relationism.
- Independent research -- running out study looking at evaluative conditioning
- Independent research -- writing a 12 page paper investigating the possibility that the resistance to extinction in evaluative conditioning that has been observed may in fact be due to alternative explanations.
Oh yeah, and I've been crazy busy at work. They essentially gave me a project to complete that involes making significant changes to an application written in Visual Basic 6 (yes, I know it's really old), but the problem is that I don't have any knowledge/experience with Visual Basic 6. This wouldn't be that much of a problem considering that I could probably learn Visual Basic 6, but the really humorous part of this project is that it's supposed to be completed in 10 days. So yeah...learn a new language, make the changes, test the changes, and roll it out to the users in 10 days. Um, no thanks.
Anyways, my point is that virtually everything on that list above is now completed. Perhaps that might explain why I've been absent from writing non-Twitter related blog posts for nearly a month.
Did I mention I'm leaving to go see HER (the infamous her) in just over 3 hours?





