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.

2 thoughts on “Day 24: Continuing on with Learning Ruby

  1. Man this is great!!! I’m basically in the same position you were in and I’m using lrthw and treehouse to help me understand ruby. I’ve noticed you haven’t been blogging lately but I hope you didn’t stop. I’m rooting for you!

    1. I am definitely still going, but have had some serious work stress that has slowed me down a bit. I took a bit of a slow down to stay sane but I am about to ramp back up soon. I still intend to make it!

Comments are closed.