Day 28: Part, Two: Some Treehouse

OK, its late and it was a long day but I am committed to continuing. Let’s get to it:

Video 1: Variables

Here is the code. Note that this is the same name.rb file from the last Ruby lesson at Treehouse. We also changed the way the puts statements at the end were written:

And the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby name.rb
Mr. Nick Teddy Queen

Challenge Task 1 of 4

In the initialize method of the Name class, set an instance variable called @title to the title argument. Note: you will have to write the initialize method.

Challenge Task 2 of 4

Below the Name class, instantiate a new Name instance set to the variable name with any title you choose.

Challenge Task 3 of 4

Inside the Name class, create a method called title that returns the @title variable.

Challenge Task 4 of 4

Call the title method on the name instance.

Video 2: Attribute Readers

The code:

Wow. That is a really neat code!

The result is the same:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby name.rb
Mr. Nick Teddy Queen

Challenge Task 1 of 1

In the Name class, create an attr_reader for the title instance variable.

Video 3: Attribute Writers and Accessors

First some work in IRB:

And having not been able to give myself a doctorate, we need to go into the file and fix this travesty!

OR, we can use attr_writer, and past that, attr_accessor which does reader and writer:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby name.rb
Mr. Nick Teddy Queen
Title: Mr.
Title: Dr.

Challenge Task 1 of 2

In the Name class, create an attr_writer for the first_name instance variable.

Challenge Task 2 of 2

In the Name class, create an attr_accessor for the last_name instance variable.

And that is it for tonight. I am halfway through this stage. Will finish tomorrow!

Day 28, Part One: Exercise 33 of Learn Ruby the Hard Way

I have some time before work so want to keep studying!

Time for Exercise 33: While Loops

And the result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex33.rb
At the top is 0
Numbers now:
0
At the bottom i is 1
At the top is 1
Numbers now:
0
1
At the bottom i is 2
At the top is 2
Numbers now:
0
1
2
At the bottom i is 3
At the top is 3
Numbers now:
0
1
2
3
At the bottom i is 4
At the top is 4
Numbers now:
0
1
2
3
4
At the bottom i is 5
At the top is 5
Numbers now:
0
1
2
3
4
5
At the bottom i is 6
The numbers:
0
1
2
3
4
5

And on to the study drills:

1. Convert this while-loop to a function that you can call, and replace 6 in the test (i < 6) with a variable.

This was hard. I ended up writing it through twice and having to go back to read the function lessons again. My first try I got this error:

Basically, it tells me I was not passing in the correct number of arguments. I was trying to define i in the function. I took that out and then called the arguments correctly, which looks like this:

And to my surprise it works! This is so important to me. I have had doubts that I was just mimicing what I read and not really learning. I had that initial feeling of “oh crap,” seeing this study drill. I then slowed down and thought through it. I read back over past lessons. I knew I had to use def and then just write inside it the while loop. From there I tweaked until it worked. And it did!

The results:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby studydrill33.rb
At the top is 0
Numbers now:
0
At the bottom i is 1
At the top is 1
Numbers now:
0
1
At the bottom i is 2
At the top is 2
Numbers now:
0
1
2
At the bottom i is 3
At the top is 3
Numbers now:
0
1
2
3
At the bottom i is 4
At the top is 4
Numbers now:
0
1
2
3
4
At the bottom i is 5
At the top is 5
Numbers now:
0
1
2
3
4
5
At the bottom i is 6

2. Use this function to rewrite the script to try different numbers.

The great thing with functions is that you can quickly change the arguments you pass to them. I went ahead and added a second argument to the function, removing widgets from inside it and passing it through in an argument. I then tried it and here is the result:

The code:

Here is the result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby studydrill33.rb
At the top is 3
Numbers now:
3
At the bottom i is 4
At the top is 4
Numbers now:
3
4
At the bottom i is 5
At the top is 5
Numbers now:
3
4
5
At the bottom i is 6
At the top is 6
Numbers now:
3
4
5
6
At the bottom i is 7
At the top is 7
Numbers now:
3
4
5
6
7
At the bottom i is 8
At the top is 8
Numbers now:
3
4
5
6
7
8
At the bottom i is 9
At the top is 9
Numbers now:
3
4
5
6
7
8
9
At the bottom i is 10
At the top is 10
Numbers now:
3
4
5
6
7
8
9
10
At the bottom i is 11
At the top is 11
Numbers now:
3
4
5
6
7
8
9
10
11
At the bottom i is 12
At the top is 12
Numbers now:
3
4
5
6
7
8
9
10
11
12
At the bottom i is 13
At the top is 13
Numbers now:
3
4
5
6
7
8
9
10
11
12
13
At the bottom i is 14
At the top is 14
Numbers now:
3
4
5
6
7
8
9
10
11
12
13
14
At the bottom i is 15
At the top is 15
Numbers now:
3
4
5
6
7
8
9
10
11
12
13
14
15
At the bottom i is 16
At the top is 16
Numbers now:
3
4
5
6
7
8
9
10
11
12
13
14
15
16
At the bottom i is 17
At the top is 17
Numbers now:
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
At the bottom i is 18
At the top is 18
Numbers now:
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
At the bottom i is 19
At the top is 19
Numbers now:
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
At the bottom i is 20

I am amazed that I am finally getting this more and more. Yay!

3. Add another variable to the function arguments that you can pass in that lets you change the + 1 on line 8 so you can change how much it increments by. and 4. Rewrite the script again to use this function to see what effect that has.

The code:

The result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby studydrill33.rb
At the top is 3
Numbers now:
3
At the bottom i is 7
At the top is 7
Numbers now:
3
7
At the bottom i is 11
At the top is 11
Numbers now:
3
7
11
At the bottom i is 15
At the top is 15
Numbers now:
3
7
11
15
At the bottom i is 19
At the top is 19
Numbers now:
3
7
11
15
19
At the bottom i is 23

And that is all the time I have. I will continue at the end of this exercise when I return later! So excited!

Return of Day 27: Back in the Treehouse

So, I have a little more time and decided to work on a little Treehouse.

Video 1: Ruby Classes

And this is just an overview of the idea of Ruby classes, which I covered in Michael Hartl’s tutorial.

Video 2: Instantiation

OK, this goes a little deeper. We discuss creating new objects and the difference between classes and instances. The act of creating an instance of a class is called instantiation. Once created, that instance is called an object. Take a look at the irb session below:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ irb
>> string = String.new
=> “”
>> string.class
=> String
>> nick = String.new(“Nick”)
=> “Nick”
>> Array.new
=> []
>> Hash.new
=> {}
>> exit

And then some code:

And the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby myclass.rb
This is the initialize method.

And the challenge:

Challenge Task 1 of 2

Instantiate a new instance of the String class and assign the newly instantiated instance to the variable string.

Challenge Task 2 of 2

Instantiate a new instance of the Array class and assign the newly instantiated instance to the variable array.

Video 3: Ruby Objects

So, object oriented programming. I have heard of it a lot but this was the first direct discussion of it.

Challenge Task 1 of 1

Using the respond_to? method, ask the string variable if it responds to the upcase method. Remember, the respond_to? method can take a string or a symbol as an argument.

Video 4: Creating a Class

The one thing I can say I like about TreeHouse is that it gives the lessons in nice, bite-sized chunks but that they are also easy to follow, even when really difficult stuff. This isn’t necessarily difficult, and it may be because its my second time doing this, but I am beginning to understand the idea of creating a class.

The code:

So, we create a class named “Name” and then defined three methods underneath it, and then called them.

This is the location in Ruby of what we created:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby name.rb
#

and here we run it and call each method:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby name.rb
Mr.
Nick
Teddy
Queen

Challenge Task 1 of 3

Instantiate an instance of the Name class and assign it to the variable name. This must come after the definition of the Name class.

Challenge Task 2 of 3

Call the puts method with the name variable’s first_name method as an argument.

Challenge Task 3 of 3

On the next line, call the puts method with the name variable’s last_name method as an argument.

Another badge!

Day 27: A Short Evening of Study

A long day of work and company coming on Thursday night which I need to do some housework to prepare for means I will have a few short nights of study. Today I am focusing on Learn Ruby the Hard Way, Exercise 32: Loops and Arrays.

Here is the code from the lesson:

And the result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex32.rb
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got pennies
I got 2
I got dimes
I got 3
I got quarters
adding 0 to the list.
adding 1 to the list.
adding 2 to the list.
adding 3 to the list.
adding 4 to the list.
adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

And the study drills:

1. Take a look at how you used (0..5) in the last for-loop. Look up Ruby’s “range operator” (.. and …) online to see what it does.

I worked with ranges at TreeHouse and I feel pretty familiar with with what it does. Read more about ranges here.

2. Change the first for number in the_count to be a more typical .each style loop like the others.

OK. I did this:

and it worked!!!!

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex32.rb
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got pennies
I got 2
I got dimes
I got 3
I got quarters
adding 0 to the list.
adding 1 to the list.
adding 2 to the list.
adding 3 to the list.
adding 4 to the list.
adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

3. Find the Ruby documentation on arrays and read about them. What other operations can you do besides the push function? Try <<, which is the same as push but is an operator. fruits << x is the same as fruits.push(x).

And it works. It is near the end. I actually thought I screwed up, broke it to make it mess up and redid it. I was shocked I got it right. I remember it from TreeHouse but it was a vague memory.

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex32.rb
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got pennies
I got 2
I got dimes
I got 3
I got quarters
adding 0 to the list.
adding 1 to the list.
adding 2 to the list.
adding 3 to the list.
adding 4 to the list.
adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

And that is it tonight!

Day 26, Part 2: Some More Treehouse and a Recap

So, first, a recap of my current efforts:

  1. I started on May 27, 2015 with Codecademy HTML and CSS course and then moved on to Michael Hartl’s Ruby on Rails tutorial. I decided after Chapter 4 that i would focus on building up my Ruby proficiency and then return to it.
  2. I went from there to Zed Shaw’s Learn Ruby the Hard Way. I am currently on Exercise 32: Loops and Arrays.
  3. I have also signed up for TreeHouse and I am in my free month at present. I just added a widget with all the badges in the sidebar.
  4. My plan is to return to Hartl’s Ruby on Rails tutorial after completing TreeHouse’s Ruby portion and Learn Ruby the Hard Way.

So, tonight I plan to finish another portion of the TreeHouse Ruby track.

First video tonight is: Part 1: Asking Questions.

The code:

and the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby contact_list.rb
What is your name? Nick
Nick

Challenge Task 1 of 1

Fill out the parse_answer method to return the answer passed in. If the kind is number, convert it to an integer using the to_i method before returning it.

Next video: Part 2: Adding Contacts

The code:

and the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby contact_list.rb
What is the person’s name? Nick
Do you want to add a phone number? (y/n) y
Enter a phone number: 555
Do you want to add a phone number? (y/n) y
Enter a phone number: 454
Do you want to add a phone number? (y/n) 343
Do you want to add a phone number? (y/n) y
Enter a phone number: 654
Do you want to add a phone number? (y/n) n
Add another? (y/n) y
What is the person’s name? Jason
Do you want to add a phone number? (y/n) y
Enter a phone number: 765
Do you want to add a phone number? (y/n) n
Add another? (y/n) n

Challenge Task 1 of 3

Assign the value of the key name to the return value of the method get_name() in the contact hash. Assume that get_name() returns a string.

I had to look up an explanation for this one and found others found it confusing. Here is the answer:

This has to do with assigning the get_name() as the return value in the hash.

Key-Values Pairs : Hash = { KEY, VALUES }

Weird, and I think it was worded weirdly.

Challenge Task 2 of 3

Assign the value of the key phone_number to the return value of the method get_phone_number() in the contact hash. Assume that get_phone_number() returns a string.

Hmm. Again, the same.

Challenge Task 3 of 3

Append the contact hash to the contact_list array.

Last video: Part 3: Printing The Contact List

The code:

And results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby contact_list.rb
What is the person’s name? Nick
Do you want to add a phone number? (y/n) y
Enter a phone number: 787
Do you want to add a phone number? (y/n) n
Add another? (y/n) y
What is the person’s name? Jason
Do you want to add a phone number? (y/n) y
Enter a phone number: 765
Do you want to add a phone number? (y/n) y
Enter a phone number: 987
Do you want to add a phone number? (y/n) n
Add another? (y/n) y
What is the person’s name? Ryan
Do you want to add a phone number? (y/n) y
Enter a phone number: 897
Do you want to add a phone number? (y/n) n
Add another? (y/n) n
___
Name: Nick
Phone: 787
___
Name: Jason
Phone: 765
Phone: 987
___
Name: Ryan
Phone: 897

Challenge Task 1 of 1

Using the each method, iterate over the contact_list array. Assign each array item to the local variable contact in the block and print out the value of the name and phone_number keys.

And I am done for tonight!

Day 26, part 1: Getting in Some Coding in the Morning

I thought I would get some studying in before breakfast. (Note: I thought I published this earlier but guess not!) Let’s get to it:

Exercise 30: Else and If

This is now my third time doing if, elsif and else statements. It is starting to sink in!

and the results:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex30.rb
We should take the cars.
Maybe we could take the trucks.
Alright, let’s just take the trucks.

1. Try to guess what elsif and else are doing.

It does different branches in the code. It provides different options depending on if the criteria is met.

2. Change the numbers of cars, people, and trucks and then trace through each if-statement to see what will be printed.

So, I switched the numbers around:

And here is the result, with the old results in italics:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex30.rb
We should take the cars.
Maybe we could take the trucks.
Alright, let’s just take the trucks.

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex30.rb
We should not take the cars.
That’s too many trucks.
Alright, let’s just take the trucks.

3. Try some more complex boolean expressions like cars > people || trucks < cars.

OK. Here is the code:

people = 60
cars = 52
trucks = 50

if cars > people
puts “We should take the cars.”
elsif cars < people puts "We should not take the cars." else puts "We can't decide." end if trucks > cars
puts “That’s too many trucks.”
elsif trucks < cars puts "Maybe we could take the trucks." else puts "We still can't decide." end if cars < people && trucks < people puts "Blame management." else puts "Fine, let's stay home them." end and the result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex30.rb
We should not take the cars.
Maybe we could take the trucks.
Blame management.

4. Above each line write an English description of what the line does.

Exercise 31: Making Decisions

OMG we are writing an adventure! And Cthulhu!

And a bunch of results because there is a lot here.

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 1
There’s a giant bear eating a cheese cake. What do you do?
1. Take the cake.
2. Scream at the bear.
> 1
The bear eats your face off. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 1
There’s a giant bear eating a cheese cake. What do you do?
1. Take the cake.
2. Scream at the bear.
> 2
The bear eats your legs off. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 1
There’s a giant bear eating a cheese cake. What do you do?
1. Take the cake.
2. Scream at the bear.
> 3
Well, doing 3 is probably better. Bear runs away.
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 2
You stare into the endless abyss at Cthulhu’s retina.
1. Blueberries.
2. Yellow jacket clothespins.
3. Understanding revolvers yelling melodies.
> 1
Your body survives powered by a mind of jello. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 2
You stare into the endless abyss at Cthulhu’s retina.
1. Blueberries.
2. Yellow jacket clothespins.
3. Understanding revolvers yelling melodies.
> 2
Your body survives powered by a mind of jello. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 2
You stare into the endless abyss at Cthulhu’s retina.
1. Blueberries.
2. Yellow jacket clothespins.
3. Understanding revolvers yelling melodies.
> 3
The insanity rots your eyes into a pool of muck. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 3
You stumble around and fall on a knife and die. Good job!

Study Drills:

1. Make new parts of the game and change what decisions people can make. Expand the game out as much as you can before it gets ridiculous.
2. Write a completely new game. Maybe you don’t like this one, so make your own. This is your computer, do what you want.

I did 1. but 2. will take some work. I really want to do this, however! Let’s add another door to the adventure!

And the results are:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1, door #2
or door #3?
> 3
You see a clown with an axe beckoning for you to come closer.
1. Go closer. Its a clown!
2. Use the clown repellent in your belt.
3. Try to run away!
> 1
The clown introduces you to his good buddy Abe the Axe. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1, door #2
or door #3?
> 3
You see a clown with an axe beckoning for you to come closer.
1. Go closer. Its a clown!
2. Use the clown repellent in your belt.
3. Try to run away!
> 2
You reach down to get the clown repellent but realize too late you are not Batman! Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1, door #2
or door #3?
> 3
You see a clown with an axe beckoning for you to come closer.
1. Go closer. Its a clown!
2. Use the clown repellent in your belt.
3. Try to run away!
> 3
You turn to run and see that Cthulu came to visit Bozo the clown. You sink to your knees in terror. Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1, door #2
or door #3?
> 3
You see a clown with an axe beckoning for you to come closer.
1. Go closer. Its a clown!
2. Use the clown repellent in your belt.
3. Try to run away!
> 6
Really, there is no good option, even 6 is bad.

And that is it until later. I want to get through

Day 25: Continuing to Learn Ruby the Hard Way in a Treehouse

I try to keep the titles interesting, but I am running out of ways to do such.

FYI, I had a lot of false starts. First time I sat down we ended up having a tornado warning where I live. Next time I finally settle down and my dog demands me take him for a walk. Thankfully, the rain had stopped. But when we got to the farthest reaches of the walk it begins a torrential downpour and soaked me completely.

During all of this I did finish Exercise 27: Memorizing Logic by creating flash cards in AnkiDroid. I will be studying them this week. So, on to Exercise 28: Boolean Practice.

In this lesson I have to guess what the answer to each of the posed Boolean problems will be and then run them. So, I will put a comment next to each and then see how I do!

Here are my guesses with the code:

and then I opened IRB and ran each. I did well and got them all. The last ones were mind-bending, but if you take your time they are doable.

demosthenes131@rails-tutorial:~/workspace $ irb
>> true && true
=> true
>> false && true
=> false
>> 1 == 1 && 2 == 1
=> false
>> “test” == “test”
=> true
>> 1 == 1 || 2 != 1
=> true
>> true && 1 == 1
=> true
>> false && 1 == 1
=> false
>> true || 1 == 1
=> true
>> “test” == “testing”
=> false
>> 1 != 0 && 2 == 1
=> false
>> “test” != “testing”
=> true
>> “test” == 1
=> false
>> !(true && false)
=> true
>> !(1 == 1 && 0 != 1)
=> false
>> !(10 == 1 || 1000 == 1000)
=> false
>> !(1 != 10 || 3 == 4)
=> false
>> !(“testing” == “testing” && “Zed” == “Cool Guy”)
=> true
>> 1 == 1 && (!(“testing” == 1 || 1 ==0))
=> true
>> “chunky” == “bacon” && (!(3 == 4 || 3 == 3))
=> false
>> 3 == 3 && (!(“testing” == “testing” || “Ruby” == “Fun”))
=> false

Study drill time!

1. and 2. here are a lot of operators in Ruby similar to != and ==. Try to find as many “equality operators” as you can. They should be like < or <=. Write out the names of each of these equality operators. For example, I call != "not equal."

Done this before. You have the following:

> Greater than
>= Greater than or equal to
< Less than <= Less than or equal to == Equal === Used in a case/ when control structure which we did in Treehouse a bit back && and || or ! not != not equal

3. Play with the Ruby by typing out new boolean operators, and before you press Enter try to shout out what it is. Do not think about it. Shout the first thing that comes to mind. Write it down, then press Enter, and keep track of how many you get right and wrong.

With my Treehouse experience I have been doing this a lot so I’m moving forward!

Exercise 29: What If

So, the nice thing is I have done this at the Hartl tutorial and the Treehouse lesson. The reinforcement in very important!

Here’s the code:

and the results:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex29.rb
Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.

And on to the study drills:

1. What do you think the if does to the code under it?

It asks Ruby to evaluate the problem asked of it, basically if the following is true, do this.

2. and 3. Why does the code under the if need to be indented two spaces? and What happens if it isn’t indented?

I am still getting used to the proper formatting, but basically, the indenting makes it readable to humans. Ruby will still run it the same, but the indenting makes the programmers life easier.

4. Can you put other boolean expressions from Exercise 27 in the if-statement? Try it.

Yep. For example:

Returns:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex29.rb
Too many cats! The world is doomed!
Not many cats! The world is saved!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.
demosthenes131@rails-tutorial:~/workspace/LRTHW $

5. What happens if you change the initial values for people, cats, and dogs?

It would change the outputs of the script. Here is an example:

and the results:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex29.rb
Not many cats! The world is saved!
The world is drooled on!
People are less than or equal to dogs.

And now on to Treehouse.

First video “Iteration With Each”

So, first we run this:

and get this:

treehouse:~/workspace$ ruby each.rb
The current item is 0.
The current item is 1.
The current item is 2.
The current item is 3.
The current item is 4.
The current item is 5.

Then we comment out a portion and write an each statement, like so:

and get the same results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby each.rb
The current array item is: 0
The current array item is: 1
The current array item is: 2
The current array item is: 3
The current array item is: 4
The current array item is: 5

And we create a second set of code to show using the each method and addition:

and the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby each_addition.rb
The current item + 2 is 2.
The current item + 2 is 3.
The current item + 2 is 4.
The current item + 2 is 5.
The current item + 2 is 6.
The current item + 2 is 7.

Challenge Task 1 of 1

Using the each method, iterate over every item in the numbers array and print the item using the puts method.

Next video is “Hash Iteration.”

Here is the code:

and the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby hash_iteration.rb
The hash key is name and the value is Treehouse.
The hash key is location and the value is Portland, OR.
Key: name
Key: location
Value: Treehouse
Value: Portland, OR

Challenge Task 1 of 1

Using the each method, iterate over every item in the contact hash and print the key and value using the puts method.

And onward to “Times Iteration.”

Here is the first bit of code:

and the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby times.rb
Hello!
Hello!
Hello!
Hello!
Hello!

and the second bit of code, using the times method with arguments:

and the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby times_with_arguments.rb
Hello! 0
Hello! 1
Hello! 2
Hello! 3
Hello! 4

Challenge Task 1 of 1

Print anything to standard output using the puts method 5 times by calling the times method on the number 5.

and the last video tonight, “For Loops”

The code:

and the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby for_loop.rb
The current item is 1.
The current item is 2.
The current item is 3.
The current item is 4.
The current item is 5.
The current item is 6.
The current item is 7.
The current item is 8.
The current item is 9.
The current item is 10.
The current item is Programming.
The current item is is.
The current item is fun.

Challenge Task 1 of 1

Use a for loop to print each item in the animals array to the screen using the puts method.

and done!

And I am finished for tonight!

Day 24: Continuing on with Learning Ruby

I am back at it on a Friday night. Let’s get coding:

First video up is Loop Conditionals

The code:

and the results:

treehouse:~/workspace$ ruby loop_conditional.rb
Guess the number between 0 and 5 (e to exit): 3
Try again!
Guess the number between 0 and 5 (e to exit): 4
You guessed correctly!
treehouse:~/workspace$ ruby loop_conditional.rb
Guess the number between 0 and 5 (e to exit): e
The number was 0.

and another one from this video:

treehouse:~/workspace$ ruby loop_conditional_number.rb
Enter a number greater than 10 to exit: 5
Enter a number greater than 10 to exit: 11

and one last one:

treehouse:~/workspace$ ruby loop_name.rb
Enter your name (minimum 2 characters, no numbers): Nick
Hi Nick.
treehouse:~/workspace$ ruby loop_name.rb
Enter your name (minimum 2 characters, no numbers): 2
Name must be longer than 2 characters and not contain numbers.
Enter your name (minimum 2 characters, no numbers): 22
Name must be longer than 2 characters and not contain numbers.
Enter your name (minimum 2 characters, no numbers): J
Name must be longer than 2 characters and not contain numbers.
Enter your name (minimum 2 characters, no numbers): Ja
Hi Ja.

Time for the challenge!

Challenge Task 1 of 1

Using a loop construct, assign the value of the get_answer() method to an answer variable. Use the break keyword to exit the loop if the answer variable is equal to the string e. Assume get_answer() is already written.

I actually had some trouble with this. For odd reasons I still don’t understand, I wanted to put a conditional. There was no reason to do this. It just wanted the answer assigned to get_answer, unconditionally. Sigh.

Next video: The While Loop

First code:

treehouse:~/workspace$ ruby while_loop.rb
Do you want me to repeat this pointless loop again? (y/n) y
Do you want me to repeat this pointless loop again? (y/n) y
Do you want me to repeat this pointless loop again? (y/n) n
treehouse:~/workspace$ ruby while_loop.rb
Do you want me to repeat this pointless loop again? (y/n) r
Do you want me to repeat this pointless loop again? (y/n) d
Do you want me to repeat this pointless loop again? (y/n) v
Do you want me to repeat this pointless loop again? (y/n) b
Do you want me to repeat this pointless loop again? (y/n) n

and the second code:

treehouse:~/workspace$ ruby while_number.rb
How many times do you want to print ‘hello’? Enter a number greater than 5 to exit) 4
hello
hello
hello
hello
How many times do you want to print ‘hello’? Enter a number greater than 5 to exit) 8
hello
hello
hello
hello
hello
hello
hello
hello

Challenge Task 1 of 1

Using a while loop, increment the value of the variable i as long as it remains less than 5.

Onward to The Until Loop

First set of code:

and results:

treehouse:~/workspace$ ruby until.rb
Do you want this loop to continue? (y/n) y
Do you want this loop to continue? (y/n) y
Do you want this loop to continue? (y/n) n

Second set, which takes the while loop above and switches it to an until:

treehouse:~/workspace$ ruby until_number.rb
How many times do you want to print ‘hello’? Enter a number greater than 5 to exit) 3
hello
hello
hello
How many times do you want to print ‘hello’? Enter a number greater than 5 to exit) 4
hello
hello
hello
hello
How many times do you want to print ‘hello’? Enter a number greater than 5 to exit) 5
hello
hello
hello
hello
hello
treehouse

Challenge Task 1 of 1

Using an until loop, increment the value of the variable i until it is greater than 5.

Alright, time for a break from Treehouse and a plunge back into Zed Shaw’s Learn Ruby the Hard Way!

Exercise 26: Congratulations, Take a Test!

Back in the deep end!

So the quiz involves fixing someone else’s code. And Mr. Shaw says it will be very hard. OK, game face!

We start with this:

I am going to post regular updates of all the errors. My plan is to read through the code, fix it and run it and sort it out systematically.

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:5: syntax error, unexpected tIDENTIFIER, expecting ‘)’
words = stuff.split(‘ ‘)
^
ex26.rb:18: syntax error, unexpected keyword_end, expecting end-of-input

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:5: syntax error, unexpected tIDENTIFIER, expecting ‘)’
words = stuff.split(”)
^
ex26.rb:18: syntax error, unexpected keyword_end, expecting end-of-input

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:5: syntax error, unexpected tIDENTIFIER, expecting ‘)’
words = stuff.split(”)
^
ex26.rb:18: syntax error, unexpected keyword_end, expecting end-of-input

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:21: syntax error, unexpected ‘:’, expecting ‘;’ or ‘\n’
def Ex25:print_last_word(words)
^
ex26.rb:24: syntax error, unexpected keyword_end, expecting end-of-input

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:34: syntax error, unexpected tIDENTIFIER, expecting ‘)’
words = Ex25.break_words(sentenc)
^
ex26.rb:79: syntax error, unexpected tCONSTANT, expecting tSTRING_DEND
puts “With a starting point of: #{start_point}”
^
ex26.rb:79: syntax error, unexpected ‘:’, expecting tSTRING_DEND
puts “With a starting point of: #{start_point}”
^
ex26.rb:88: syntax error, unexpected ‘)’, expecting tSTRING_DEND
ex26.rb:93: syntax error, unexpected ‘(‘, expecting tSTRING_DEND
Ex25:print_first_and_last_sorted(sentence)

^

I am sort of surprised how far it goes down in showing errors. I figured it would stop at the topmost one.

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:34: syntax error, unexpected tIDENTIFIER, expecting ‘)’
words = Ex25.break_words(sentenc)
^
ex26.rb:79: syntax error, unexpected tCONSTANT, expecting tSTRING_DEND
puts “With a starting point of: #{start_point}”
^
ex26.rb:79: syntax error, unexpected ‘:’, expecting tSTRING_DEND
puts “With a starting point of: #{start_point}”
^
ex26.rb:88: syntax error, unexpected ‘)’, expecting tSTRING_DEND
ex26.rb:93: syntax error, unexpected ‘(‘, expecting tSTRING_DEND
Ex25:print_first_and_last_sorted(sentence)
^

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:34: syntax error, unexpected tIDENTIFIER, expecting ‘)’
words = Ex25.break_words(sentenc)
^
ex26.rb:79: syntax error, unexpected tCONSTANT, expecting tSTRING_DEND
puts “With a starting point of: #{start_point}”
^
ex26.rb:79: syntax error, unexpected ‘:’, expecting tSTRING_DEND
puts “With a starting point of: #{start_point}”
^
ex26.rb:88: syntax error, unexpected ‘)’, expecting tSTRING_DEND
ex26.rb:93: syntax error, unexpected ‘(‘, expecting tSTRING_DEND
Ex25:print_first_and_last_sorted(sentence)
^

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:79: syntax error, unexpected tCONSTANT, expecting tSTRING_DEND
puts “With a starting point of: #{start_point}”
^
ex26.rb:79: syntax error, unexpected ‘:’, expecting tSTRING_DEND
puts “With a starting point of: #{start_point}”
^
ex26.rb:88: syntax error, unexpected ‘)’, expecting tSTRING_DEND
ex26.rb:93: syntax error, unexpected ‘(‘, expecting tSTRING_DEND
Ex25:print_first_and_last_sorted(sentence)
^

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:65: syntax error, unexpected tINTEGER, expecting keyword_end
five = 10 – 2 3 – 6
^
ex26.rb:77: syntax error, unexpected tIDENTIFIER, expecting keyword_do or ‘{‘ or ‘(‘
beans, jars crates = secret_formula(start_point)
^
ex26.rb:88: syntax error, unexpected ‘)’, expecting tSTRING_DEND
ex26.rb:93: syntax error, unexpected ‘(‘, expecting tSTRING_DEND
Ex25:print_first_and_last_sorted(sentence)
^

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:93: syntax error, unexpected ‘(‘, expecting tSTRING_DEND
Ex25:print_first_and_last_sorted(sentence)
^

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:93: syntax error, unexpected end-of-input, expecting tSTRING_DEND
Ex25.print_first_and_last_sorted(sentence)
^

Nick@WASHBUCKET /c/ruby
$ ruby ex26.rb
ex26.rb:93: syntax error, unexpected end-of-input, expecting tSTRING_DEND
Ex25.print_first_and_last_sorted(sentence)

Kind of stuck. Sigh… I am moving to Cloud9. I like the editor there better…

demosthenes131@rails-tutorial:~/workspace $ ruby ex26.rb
ex26.rb:93: syntax error, unexpected end-of-input, expecting tSTRING_DEND
Ex25.print_first_and_last_sorted(sentence)
^
demosthenes131@rails-tutorial:~/workspace $ ruby ex26.rb
Let’s practice everything.
You’d need to know ’bout escapes with \ that do \n newlines and \t tabs.
————–
The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explanation

where there is none.
————–
This should be five: 5.
With a starting point of: 10000.
ex26.rb:80:in

': undefined method beans’ for main:Object (NoMethodError)
demosthenes131@rails-tutorial:~/workspace $ ruby ex26.rb

It was a }

a simple

}

One last fix…

and it works…

demosthenes131@rails-tutorial:~/workspace $ ruby ex26.rb
Let’s practice everything.
You’d need to know ’bout escapes with \ that do \n newlines and \t tabs.
————–
The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explanation

where there is none.
————–
This should be five: 5.
With a starting point of: 10000.
We’d have 5000000, 5000 jars, and 50 crates.
wait.
who
who
wait.
wait.
who
who
wait.

I am calling it a night.

Day 23: Playing in the Treehouse

I’m back at the Ruby Learning game, friends. Let’s jump in! I want to get through a lot, so I am going to hustle.

I am on the last stage of the Ruby Collections course, Build a Grocery List Program.

First video is Build a Grocery List Program: Part 1.

Here is the code:

Results:

treehouse:~/workspace$ ruby shopping_list.rb
What is the list name?Groceries
{“name”=>”Groceries”, “items”=>[]}

And the challenge:

Challenge Task 1 of 1

Create a method named “create_shopping_list” that returns a hash. It does not need to ask for a name or get anything from standard input.

On to the next video! Build a Grocery List Program: Part 2

And the results:

treehouse:~/workspace$ ruby shopping_list.rb
What is the list name? Groceries
{“name”=>”Groceries”, “items”=>[]}
What is the item called? Milk
How much? 1
{“name”=>”Groceries”, “items”=>[{“name”=>”Milk”, “quantity”=>1}]}

and the challenge:

Challenge Task 1 of 1

Create a method called add_list_items that returns an array. It does not need to read from standard input.

And the next video: Build a Grocery List Program: Part 3

And the results:

treehouse:~/workspace$ ruby shopping_list.rb
What is the list name? Groceries
{“name”=>”Groceries”, “items”=>[]}
What is the item called? Milk
How much? 2
{“name”=>”Groceries”, “items”=>[{“name”=>”Milk”, “quantity”=>2}]}
List: Groceries
—-
Item: Milk
Quantity: 2

and the challenge:

Challenge Task 1 of 1

Modify the “create_shopping_list” method to return a hash with the following keys and values:
‘title’: A string with the value “Grocery List”
‘items’: An empty array

Just to pause here. I do feel like I am learning a lot in this course. I am actually able to read the challenges and know what to do pretty quickly. Just a thought!

Onward! Build a Grocery List Program: Part 4

The code:

The results:

treehouse:~/workspace$ ruby shopping_list.rb
What is the list name? Groceries
Great! Add some items to your list.
What is the item called? Milk
How much? 1
What is the item called? Bread
How much? 2
What is the item called? Eggs
How much? 3
Here’s your list:
List: Groceries
——————————————————————————–
Item: Milk Quantity: 1
Item: Bread Quantity: 2
Item: Eggs Quantity: 3
——————————————————————————–

The challenge:

Challenge Task 1 of 1

Add the grocery_item hash to the items array inside of the grocery_list hash.

And after the quiz:

ON TO RUBY LOOPS!

First video:

and the result:

treehouse:~/workspace$ ruby loop.rb
Do you want to continue? (y/n) y
Do you want to continue? (y/n) y
Do you want to continue? (y/n) y
Do you want to continue? (y/n) n
treehouse:~/workspace$

The challenge question:

Using the loop construct, add the current value of number to the numbers array. Inside of the loop, add 1 to the number variable. If the numbers array has more than 3 items, use the break keyword to exit the loop.

Well, I’m ending here for tonight. My goals yesterday were pretty lofty. I hope to finish the loops portion tomorrow!

Day 22: Back in the Treehouse

I’m back and wanting to knock out a bunch more work tonight. Let’s get to it!

I’m starting with Treehouse. I did a lesson earlier on arrays. I am continuing onward from there.

Here is the code I did, which looks at adding items to an array:

and the results:

treehouse:~/workspace$ ruby array_addition.rb
[“milk”, “eggs”, “bread”, “carrots”]
treehouse:~/workspace$ ruby array_addition.rb
[“celery”, “milk”, “eggs”, “bread”, “carrots”, “potatoes”]
treehouse:~/workspace$ ruby array_addition.rb
[“celery”, “milk”, “eggs”, “bread”, “carrots”, “potatoes”, “ice cream”, “pie”]

This was as I added in lines and kept re-running it.

And the challenge:

Add the string “carrots” to the end of the grocery_list array.

and my work:

And the challenge:

Challenge Task 1 of 3

Create a variable called “first_item” that contains the first item from the “grocery_list” array (index 0).

Challenge Task 2 of 3

Create a variable called “last_item” that contains the last item from the “grocery_list” array. You can use either the method last or the index.

Challenge Task 3 of 3

Almost there! Set a variable called “list_size” that contains the number of items in the array. You can use the “count” method for this.

Next video: “Removing Items From Arrays”

The IRB commands:

Challenge Task 1 of 2

Set a variable called last_item which uses the pop method to take the last item out of the grocery_list array.

Challenge Task 2 of 2

Set a variable called first_item which uses the shift method to take the first item out of the grocery_list array.

Next section, first video: Ruby Hash Creation

Challenge Task 1 of 2

Create a variable called grocery_item_1 which is a new hash.

Challenge Task 2 of 2

Create a key in the grocery_item_1 hash called “name” and set the value of that key to the string “milk”.

grocery_item_1 = {“name” => “milk”}

Next video: “Working with Hash Keys”

My work in IRB: