Day 34: Studying Hashes

OK, my Book Tracker app is coming along but I am having difficulty getting the hash to output correctly.

I tried one last go at it this morning:

and it resulted in this:

demosthenes131@book_tracker:~/workspace (master) $ ruby test.rb
Do you want to add a book? (yes or no)
yes
Add a book!
Ender
How about a short, one sentence review? (yes or no)
yes
Add your review!
Awesome
Do you want to add a book? (yes or no)
yes
Add a book!
Dresden
How about a short, one sentence review? (yes or no)
yes
Add your review!
Amazing
Do you want to add a book? (yes or no)
no
[“Ender”, “Dresden”]
{“book”=>[[“Ender”, “Dresden”]], “review”=>[[“Awesome”, “Amazing”]]}
book — [[“Ender”, “Dresden”]]
review — [[“Awesome”, “Amazing”]]

Close, but not what I want. Time to look for a solution.

I worked out a simple one to get the look and feel down. Take a look:

demosthenes131@book_tracker:~/workspace (master) $ ruby test.rb
Do you want to add a book? (yes or no)
yes
Add a book!
Ender
How about a short, one sentence review? (yes or no)
yes
Add your review!
Awesome
Do you want to add a book? (yes or no)
yes
Add a book!
Dresden
How about a short, one sentence review? (yes or no)
yes
Add your review!
Amazing
Do you want to add a book? (yes or no)
no
Ender — Awesome
Dresden — Amazing

Day 31: A Quick Treehouse Dip

I didn’t get to come back yesterday, but getting a chance to do a small lesson today. I have family in and will being spending time with them.

First video: What is a Boolean value?

Which is a quick overview of Boolean values.

Challenge Task 1 of 2

Write an if statement that calls the flash_car_speed method if the car_speed variable is greater than the speed_limit variable.
Assume that the flash_car_speed and display_car_speed methods have been defined elsewhere.

Challenge Task 2 of 2

Add an else clause to the if statement which calls the display_car_speed method.

Oh. I did it already…

Challenge Task 1 of 1

Using the negation operator, reverse the logic in the boolean statement.

Day 21, Part 1: Doing Treehouse to Brush Up

So, this morning I am going quickly through the Ruby Basics track on Treehouse. I have had 2 prior go’s at learning Ruby and this is a great brush up to reinforce my skills. I have learned a few things though, such as ** is used for exponents.

I finished the Ruby Basics course and I started on the “Ruby Operators and Control Structures” course. I will continue tonight plus read some code at Github to continue the Learn Ruby the Hard Way book.

Day 17 Continued: I Keep on Learning Ruby the Hard Way

I’m back!

Exercise 13: Parameters, Unpacking, Variables

This exercise starts out talking about ARGV. Mr. Shaw calls it an “argument variable.” It holds the arguments you pass to the Ruby script when it is ran.

I typed this:

And this happens:

PS C:\ruby> ruby ex13.rb
Your first variable is
Your second variable is
Your third variable is

And I was a bit confused…

I went and read again. Mr. Shaw says:

Run the program like this (and you must pass three command line arguments):

$ ruby ex13.rb first 2nd 3rd

Oh… I need to read closer! Let’s try again:

PS C:\ruby> ruby ex13.rb first 2nd 3rd
Your first variable is first
Your second variable is 2nd
Your third variable is 3rd

Good… Reading on, you can replace the “first 2nd 3rd” with whatever you want.

PS C:\ruby> ruby ex13.rb Larry Moe Curly
Your first variable is Larry
Your second variable is Moe
Your third variable is Curly

I understand that you are passing on variable from the command line. The order you place them in is how they are distributed. I just don’t know if I completely understand why, yet.

Study Drills!

1. Try giving fewer than three arguments to your script.

Easy.

PS C:\ruby> ruby ex13.rb first 2nd
Your first variable is first
Your second variable is 2nd

2. Write a script that has fewer arguments and one that has more. Make sure you give the unpacked variables good names.

Well, see above for the first part. Here is more:

PS C:\ruby> ruby ex13.rb Batman Superman WonderWoman GreenArrow Flash Aquaman
Your first variable is Batman
Your second variable is Superman
Your third variable is WonderWoman
Your fourth variable is GreenArrow
Your fifth variable is Flash
Your sixth variable is Aquaman

OK, this next one frustrated me to no end.

3. Combine gets.chomp with ARGV to make a script that gets more input from a user. text

Simple enough. I know gets.chomp…

Um…

PS C:\ruby> ruby ex13.rb Batman Superman WonderWoman GreenArrow Flash Aquaman
Your first variable is Batman
Your second variable is Superman
Your third variable is WonderWoman
Your fourth variable is GreenArrow
Your fifth variable is Flash
Your sixth variable is Aquaman
Who is your favorite superhero?ex13.rb:11:in gets': No such file or directory @ rb_sysopen - Batman (Errno::ENOENT)
from ex13.rb:11:in
gets’
from ex13.rb:11:in

'

Well, that is odd. I decide to try to call the 6 variables in my puts at the end.

PS C:\ruby> ruby ex13.rb Batman Superman WonderWoman GreenArrow Flash Aquaman
Your first variable is Batman
Your second variable is Superman
Your third variable is WonderWoman
Your fourth variable is GreenArrow
Your fifth variable is Flash
Your sixth variable is Aquaman
Who is your favorite superhero?ex13.rb:11:in
gets’: No such file or directory @ rb_sysopen – Batman (Errno::ENOENT)
from ex13.rb:11:in gets'
from ex13.rb:11:in

But why! I shamelessly looked to the next exercise. Mr. Shaw, you are the devil!

PS C:\ruby> ruby ex13.rb Batman Superman WonderWoman GreenArrow Flash Aquaman
Your first variable is Batman
Your second variable is Superman
Your third variable is WonderWoman
Your fourth variable is GreenArrow
Your fifth variable is Flash
Your sixth variable is Aquaman
Who is your favorite superhero?Moon Knight
Oh, you like Moon Knight the most? That’s cool, I guess.
I think Batman, Superman, WonderWoman, GreenArrow, Flash and Aquaman are better.

I guess we will learn about STDIN next exercise.

Exercise 14: Prompting and Passing

I typed:

And the result:

PS C:\ruby> ruby ex14.rb Nick
Hi Nick.
I’d like to ask you a few questions.
Do you like me Nick?
>
sure
Where do you live Nick?
>
Baltimore
What kind of computer do you have?
>
Sager

Alright, so you said sure about liking me.
You live in Baltimore. Not sure where that is.
And you have a Sager computer. Nice.

Hmm. Interesting.

STUDY DRILLS!

1. Find out what Zork and Adventure were. Try to find a copy and play it.

I preferred Hitchhiker’s Guide to the Galaxy! Click here to try Zork, and here to try Adventure.

2. Change the prompt variable to something else entirely.

And the result:

PS C:\ruby> ruby ex14.rb Nick
Hi Nick.
I’d like to ask you a few questions.
Do you like me Nick?
==>
No
Where do you live Nick?
==>
Baltimore
What kind of computer do you have?
==>
Sager

Alright, so you said No about liking me.
You live in Baltimore. Not sure where that is.
And you have a Sager computer. Nice.

3. Add another argument and use it in your script, the same way you did in the previous exercise with first, second = ARGV.

This was kind of hard.

PS C:\ruby> ruby ex14.rb Nick Master
ex14.rb:2:in

': undefined method second’ for [“Nick”, “Master”]:Array (NoMethodError)

My thinking was that last exercise we used first, second, third and that I needed to follow suit. This called the ARGV differently. Off I go to the Google!

TestHead had the answer. He describes this as an array, and used .last instead of .second… Well, that makes sense!

PS C:\ruby> ruby ex14.rb Nick Master
Hi Master Nick.
I’d like to ask you a few questions.
Do you like me Master Nick?
==>
a little bit
Where do you live Master Nick?
==>
Baltimore
What kind of computer do you have?
==>
Sager

Alright, Master, so you said a little bit about liking me.
You live in Baltimore. Not sure where that is.
And you have a Sager computer. Nice.

That works!

4. Make sure you understand how I combined a “”” style multiline string with the #{} format activator as the last print.

See above. i added one in to the paragraph for the ARGV.last.

Exercise 15: Reading Files

What I typed:

And the result:

PS C:\ruby> ruby ex15.rb ex15_sample.txt
Here’s your file ex15_sample.txt:
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.Type the filename again: ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.PS C:\ruby>

So, the ARGV is used to get the filename. We also have a new command: open

And, a new method, .read

Lots of fun stuff going on here.

Study drills time!

1. Above each line, write out in English what that line does.

2. If you are not sure ask someone for help or search online. Many times searching for “ruby THING” will find answers to what that THING does in Ruby. Try searching for “ruby open.”

A couple of examples

3. I used the word “commands” here, but commands are also called “functions” and “methods.” You will learn about functions and methods later in the book.

Yeah, I am use to the term methods.

4. Get rid of the lines 8-13 where you use gets.chomp and run the script again.

So, only this is in the file:

PS C:\ruby> ruby ex15.rb ex15_sample.txt
Here’s your file ex15_sample.txt:
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.PS C:\ruby>

5. Use only gets.chomp and try the script that way. Why is one way of getting the filename would be better than another?

So, only this in the file:

And this is the result:

PS C:\ruby> ruby ex15.rb
Type the filename again: ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.PS C:\ruby>

I think the first method is more direct, but either is effective in the end.

6. Start irb to start the Ruby shell, and use open from the prompt just like in this program. Notice how you can open files and run read on them from within irb?

I REALLY beat my head on the wall with this one. I fianlly realized after reading numerous posts on stackoverflow that I wasn’t use the damn .read! Grr. Here is what finally worked:

irb(main):019:0> File.open(“ex15_sample.txt”)
=> #
irb(main):020:0> puts txt.read()
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
=> nil

7. Have your script also call close() on the txt and txt_again variables. It’s important to close files when you are done with them.

I cannot seem to get close to work. I read for awhile on StackOverflow and other sites, but had no luck! Looking ahead, it looks like Mr. Shaw will cover this. I am moving on in hopes of better understanding!

I am also calling it a night. I am beat.

Day 17: Continuing with Learning Ruby the Hard Way

Exercise 12: Prompting People for Numbers

Here is what I had to type:

And the result:

PS C:\ruby> ruby ex12.rb
Give me a number: 42
A bigger number is 4200.
Give me another number: 33
A smaller number is 0.

This builds on the gets.chomp from the last exercise. By adding .to_i to the method, it converts what is entered to an integer. The second number asked for is first done through gets.chomp and then converted to an integer with .to_i.

Study Drills:

1. Try out the .to_f operation. What does .to_f do?

So, I typed this:

and got this:

PS C:\ruby> ruby ex12.rb
Give me a number: 100
A bigger number is 10000.0.
Give me another number: 1000
A smaller number is 10.0.

Looks like it converts the number to a floating decimal!

2. To play with .to_f more, make a small script that asks for some money and gives back 10% of it. If I give your script 103.4 (dollars), your script gives me back 10.34 in change.

SO, this one was a bit weird. i tried originally to use:

PS C:\ruby> ruby studydrill12.rb
We can help you with paying the right tip! How much is your bill?
36.98
OK, so a typical tip is 20% of the bill. That means your tip would be $7.396. The total is $44.376.

This is great, but I want Ruby to do the rounding. After Googling for a bit I found .round() and changed the code to this:

and the result:

PS C:\ruby> ruby studydrill12.rb
We can help you with paying the right tip! How much is your bill?
36.98
OK, so a typical tip is 20% of the bill. That means your tip would be $7.4. The total is $44.38.

The only issue right now is that if you enter letters you get this:

PS C:\ruby> ruby studydrill12.rb
We can help you with paying the right tip! How much is your bill?
hi everyone
OK, so a typical tip is 20% of the bill. That means your tip would be $0.0. The total is $0.0.

So, I need to work on getting it to validate integers and not letters! Baby steps.

I am off to eat and will be back later!

Day 14: Changing it Up for a Bit

I had thoughts all day about whether I should focus on Ruby for a bit or move on to the rest of the Ruby on Rails tutorial. I decided after some advice at the codenewbies Slack channel to hone my Ruby skills a bit more. Gregory Brown of PracticingRuby.com made a good point that I could go through Zed Shaw’s highly recommended book Learn Ruby the Hard Way while using the other highly recommended book, Chris Pine’s Learn to Program for context. I decided to move ahead for a bit doing this.

So, I spent some time setting up Ruby on my computer, as well as tinkering in Power Shell. It has been a long time since I used Power Shell.

And then I started with Exercise One. One nice thing about getting through 4 chapters of the Rails tutorial is that I understand what I am doing here. So, Exercise 1: A Good First Program. I opened up Sublime (Mr. Shaw suggested Notepad ++ but I had Sublime installed prior and wanted to try it. I typed in the following:

Then went to Power Shell and ran this:

PS C:\ruby> ruby ex1.rb
Hello World!
Hello Again
I like typing this.
This is fun.
Yay! Printing!
I’d much rather you ‘not’.
I “said” do not touch this.
PS C:\ruby>

Right now I know the answer to Study Drill 3, and I may know the other two, but not sure. Number 3 talks about what happens if you put a hashtag at the front of the line. It comments the line out! I will come back to the Study Drill…

Exercise 2: Comments and Pound Characters

I typed in:

And got this:

PS C:\Users\Nick> cd\
PS C:\> cd ruby
PS C:\ruby> ruby ex2.rb
I could have code like this.
This will run.
PS C:\ruby>

Exercise 3: Numbers and Math

I will say that this tutorial has me typing a lot. I think this is good. Here is what I typed up for the exercise:

And the result once I ran it:

PS C:\ruby> ruby ex3.rb
I will now count my chickens:
Hens 30
Roosters 97
7
Is it true that 3 + 2 < 5 - 7? false What is 3 + 2? 5 What is 5 - 7? -2 Oh, that's why it's false. How about some more? Is it greater? true Is it greater or equal? true Is it less or equal? false PS C:\ruby>

OK, Study Drill. Zed is intense.

1. Above each line, use the # to write a comment to yourself explaining what the line does.

2. Remember in Exercise 0 when you started Ruby? Start Ruby this way again and using the math operators, use Ruby as a calculator.

OK< I know how to do this pretty easily. Example:

PS C:\ruby> 3 + 3
6
PS C:\ruby> 18 % 5
3

3. Find something you need to calculate and write a new .rb file that does it.

Hmm, ok I give in tonight. I will start here tomorrow.