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.