Learn To Program (Chris Pine) - Building and Sorting Array

Hi,

Super new to programming (1 week or so) and working through 'Learn to
Program' to get an understanding of Ruby before moving on to trying my
hand at Ruby on Rails. That being said I was having a bit of trouble
working through the first "A Few Things To Try" in section 8.3.

So, we're asked to write a program that asks us to type as many words as
we want (one word per line, continuing until we just press 'ENTER' on an
empty line) and then repeats the words back to us in an alphabetical
order.

After working on this for a bit, I came up with this program (see
below), which seems to work. However I really want to understand
everything and I don't think I fully get why it allows me to add things
to the array and doesn't start to sort until I press enter on a blank
line when i say if response != ' ' , but not when I make response == '
'. Basically, I don't think I get what I'm making response equal to, is
it an empty string, or?

Any help would be appreciated, as well as better ideas on how to go
about writing this program.

Thanks a ton,

Emeka

responses = []

while true
response = gets.chomp

if response != ''
responses.push response

else
  puts responses.sort
  break
end
end

···

--
Posted via http://www.ruby-forum.com/.

Super new to programming (1 week or so) and working through 'Learn to
Program' to get an understanding of Ruby before moving on to trying my
hand at Ruby on Rails. That being said I was having a bit of trouble
working through the first "A Few Things To Try" in section 8.3.

Welcome to the world of Ruby!

So, we're asked to write a program that asks us to type as many words as
we want (one word per line, continuing until we just press 'ENTER' on an
empty line) and then repeats the words back to us in an alphabetical
order.

After working on this for a bit, I came up with this program (see
below), which seems to work. However I really want to understand
everything and I don't think I fully get why it allows me to add things
to the array and doesn't start to sort until I press enter on a blank
line when i say if response != ' ' , but not when I make response == '
'. Basically, I don't think I get what I'm making response equal to, is
it an empty string, or?

!= and == are tests and not assignments. If you exchange == for !=
you also need to exchange branches of the if else end construct.

Any help would be appreciated, as well as better ideas on how to go
about writing this program.

Thanks a ton,

Emeka

responses =

while true
response = gets.chomp

if response != ''
responses.push response

else
puts responses.sort
break
end
end

As an additional task you could try to rewrite your program to not use
an endless loop ("while true") but rather use the fact that the input
was the empty string as termination condition for the loop. This
would be more natural than breaking out of the loop. The result will
look something like this:

responses =
???

while ???
  responses.push response
end

puts responses.sort

Where "???" are placeholders your code.

Kind regards

robert

···

On Fri, Dec 9, 2011 at 9:58 AM, Emeka Patrick <emekapatrick@gmail.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Hi Robert,

Thanks so much for getting back to me on this much appreciated. Some
questions in-line below.

Robert Klemme wrote in post #1035877:

Super new to programming (1 week or so) and working through 'Learn to
Program' to get an understanding of Ruby before moving on to trying my
hand at Ruby on Rails. That being said I was having a bit of trouble
working through the first "A Few Things To Try" in section 8.3.

Welcome to the world of Ruby!

'. Basically, I don't think I get what I'm making response equal to, is
it an empty string, or?

!= and == are tests and not assignments. If you exchange == for !=
you also need to exchange branches of the if else end construct.

So basically with != and == I'm asking if what precedes the expression
is either "different from" or "the same as" whatever follows. Am I
correct?

Also, I still don't think I fully understand the empty string. In the
two lines of code...

if response != ''
responses.push response

...am I doing something like "Is what was entered for the variable
response different from an empty string? If so then push whatever string
is entered to the end of the responses array."

response = gets.chomp

if response != ''
responses.push response

else
puts responses.sort
break
end
end

As an additional task you could try to rewrite your program to not use
an endless loop ("while true") but rather use the fact that the input
was the empty string as termination condition for the loop. This
would be more natural than breaking out of the loop. The result will
look something like this:

responses =
???

while ???
  responses.push response
end

puts responses.sort

Where "???" are placeholders your code.

It seemed to me that the logical way to enter this code would be

responses =
response = gets.chomp

while response != ''
  responses.push response
end

puts responses.sort

However, it's not working. Any ideas what I'm missing with this?

Kind regards

robert

Thanks again,

Emeka

···

On Fri, Dec 9, 2011 at 9:58 AM, Emeka Patrick <emekapatrick@gmail.com> > wrote:

--
Posted via http://www.ruby-forum.com/\.

Good Day Emeka,

Hi Robert,

Thanks so much for getting back to me on this much appreciated. Some
questions in-line below.

Thank you for at least attempting to figure things out. Very refreshing in
contrast to some of the questions that people pose here........

Robert Klemme wrote in post #1035877:
>> Super new to programming (1 week or so) and working through 'Learn to
>> Program' to get an understanding of Ruby before moving on to trying my
>> hand at Ruby on Rails. That being said I was having a bit of trouble
>> working through the first "A Few Things To Try" in section 8.3.
>
> Welcome to the world of Ruby!
>
>> '. Basically, I don't think I get what I'm making response equal to, is
>> it an empty string, or?
>
> != and == are tests and not assignments. If you exchange == for !=
> you also need to exchange branches of the if else end construct.

So basically with != and == I'm asking if what precedes the expression
is either "different from" or "the same as" whatever follows. Am I
correct?

That would be correct.

Also, I still don't think I fully understand the empty string. In the
two lines of code...

if response != ''
responses.push response

...am I doing something like "Is what was entered for the variable
response different from an empty string? If so then push whatever string
is entered to the end of the responses array."

That would also be correct. One other small item, normally code is indented
(Ruby standard is 2 spaces) another level inside a control statement
(if/while/else and such) to make it slightly easier to read the code.

Taking your earlier work

if response != ''
  responses.push response
else
  puts responses.sort
  break
end

It seemed to me that the logical way to enter this code would be

responses =
response = gets.chomp

while response != ''
  responses.push response
end

puts responses.sort

Actually very close here. Your problem is that you end up in an endless
loop because you never get another entry from the user.

This should do the trick

responses =
response = gets.chomp

while response != ''
  responses.push response
  response = gets.chomp # get another line from the user
end

puts responses.sort

But very very close for someone just picking up programming. You appear to
at least grasp the basic mechanics of working through a problem!

Best of luck to you!

John

···

On Fri, Dec 9, 2011 at 12:47 PM, Emeka Patrick <emekapatrick@gmail.com>wrote:

> On Fri, Dec 9, 2011 at 9:58 AM, Emeka Patrick <emekapatrick@gmail.com> > > wrote:

Hi John,

Thanks so much for getting back to me and for your kind words. Really
big encouragement for someone just starting out and feeling a wee bit
lost at times. As well, I must say the willingness of people in the Ruby
community to offer help has really been pretty amazing.

John W Higgins wrote in post #1036028:

Good Day Emeka,

Hi Robert,

Thanks so much for getting back to me on this much appreciated. Some
questions in-line below.

Thank you for at least attempting to figure things out. Very refreshing
in
contrast to some of the questions that people pose here........

Thanks a bunch. Want to make sure I truly understand how and why things
work when they do (or when they don't!)

>
>> '. Basically, I don't think I get what I'm making response equal to, is
>> it an empty string, or?
>
> != and == are tests and not assignments. If you exchange == for !=
> you also need to exchange branches of the if else end construct.

So basically with != and == I'm asking if what precedes the expression
is either "different from" or "the same as" whatever follows. Am I
correct?

That would be correct.

Great!

Also, I still don't think I fully understand the empty string. In the
two lines of code...

if response != ''
responses.push response

...am I doing something like "Is what was entered for the variable
response different from an empty string? If so then push whatever string
is entered to the end of the responses array."

That would also be correct. One other small item, normally code is
indented
(Ruby standard is 2 spaces) another level inside a control statement
(if/while/else and such) to make it slightly easier to read the code.

I've been trying to do this as well as line up certain elements of code,
but keep slipping in my enthusiasm to get it all out there. Def. need to
try harder to keep it up. What about using Tab to indent, is there any
reason that might be frowned upon? Btw, I'm using TextMate.

Taking your earlier work

if response != ''
  responses.push response
else
  puts responses.sort
  break
end

It seemed to me that the logical way to enter this code would be

responses =
response = gets.chomp

while response != ''
  responses.push response
end

puts responses.sort

Actually very close here. Your problem is that you end up in an endless
loop because you never get another entry from the user.

This should do the trick

responses =
response = gets.chomp

while response != ''
  responses.push response
  response = gets.chomp # get another line from the user
end

puts responses.sort

Oh wow, ok, I think I get it. This is super clean too. Much nicer than
what I originally had. Now, one last question, I need response =
gets.chomp in the first instance just to define what the variable stands
for, correct? It's not actually until the second instance where I'm
soliciting input from the user, or actually is it more so the first
instance both defines the variable and solicits input from the user
while the second instance solicits input in a continuous loop until it
gets broken, i.e. when a user enters an empty string?

But very very close for someone just picking up programming. You appear
to
at least grasp the basic mechanics of working through a problem!

Best of luck to you!

John

Thanks again John, super helpful! Almost done with chapter 9, "Writing
Your Own Methods". Only 100 pages to go!

On another note, do you have any recommendations for Ruby resources
and/or a path to take towards Ruby On Rails? I'm planning on finishing
Learning to Program, doing the Rails for Zombies courses, then diving
into Michael Hartl's tutorial for Rails 2.3 and then reading Agile
Development with Rails 3rd edition. Thoughts? Btw, I'm learning 2.3 as a
site I had built was in 2.3.8 and I figure I should learn that first and
then later learn 3 / 3.1.

Thanks,

Emeka

···

On Fri, Dec 9, 2011 at 12:47 PM, Emeka Patrick > <emekapatrick@gmail.com>wrote:

--
Posted via http://www.ruby-forum.com/\.

Thanks so much for getting back to me and for your kind words. Really
big encouragement for someone just starting out and feeling a wee bit
lost at times. As well, I must say the willingness of people in the Ruby
community to offer help has really been pretty amazing.

It's among the most friendly online communities I have come across -
if not the most friendly.

That would also be correct. One other small item, normally code is
indented
(Ruby standard is 2 spaces) another level inside a control statement
(if/while/else and such) to make it slightly easier to read the code.

I've been trying to do this as well as line up certain elements of code,
but keep slipping in my enthusiasm to get it all out there. Def. need to
try harder to keep it up. What about using Tab to indent, is there any
reason that might be frowned upon? Btw, I'm using TextMate.

I think people don't like the large indent especially when posting
code in emails or forums because there usually line width is limited
or lines are wrapped automatically.

Oh wow, ok, I think I get it. This is super clean too. Much nicer than
what I originally had. Now, one last question, I need response =
gets.chomp in the first instance just to define what the variable stands
for, correct?

Partly. The more important aspect in this case is that you need to
have a value which is checked as while condition.

It's not actually until the second instance where I'm
soliciting input from the user, or actually is it more so the first
instance both defines the variable and solicits input from the user
while the second instance solicits input in a continuous loop until it
gets broken, i.e. when a user enters an empty string?

Exactly (the latter). You could as well do this avoiding the
duplicate "gets.chomp":

responses =

while (response = gets.chomp) != ''
  responses << response
end

puts responses.sort

Kind regards

robert

···

On Sat, Dec 10, 2011 at 12:02 AM, Emeka Patrick <emekapatrick@gmail.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

It is one thing reading Chris Pines book and then jumping into rails but, Christ Pines book is dated, also it only deals with "a little" Ruby. Before learning Rails I would suggest you first learn how to create the "page", widgets and so on. You should try the gem RXRuby to learn to create password boxes etc. RXRuby may have come with your Ruby installation.Why I say you should learn how to create "page" and widgets is simple, you will have to do this with Rails.The "page" is basically pages that appear on the screen, these could be graphics, or form type pages.There are a few beginners Ruby lessons on oldkingjames.org that may help you with your Ruby as these are for complete beginners to programming itself.

···

Date: Sat, 10 Dec 2011 08:02:02 +0900
From: emekapatrick@gmail.com
Subject: Re: Learn To Program (Chris Pine) - Building and Sorting Array
To: ruby-talk@ruby-lang.org

Hi John,

Thanks so much for getting back to me and for your kind words. Really
big encouragement for someone just starting out and feeling a wee bit
lost at times. As well, I must say the willingness of people in the Ruby
community to offer help has really been pretty amazing.

John W Higgins wrote in post #1036028:
> Good Day Emeka,
>
> On Fri, Dec 9, 2011 at 12:47 PM, Emeka Patrick > > <emekapatrick@gmail.com>wrote:
>
>> Hi Robert,
>>
>> Thanks so much for getting back to me on this much appreciated. Some
>> questions in-line below.
>>
>
> Thank you for at least attempting to figure things out. Very refreshing
> in
> contrast to some of the questions that people pose here........

Thanks a bunch. Want to make sure I truly understand how and why things
work when they do (or when they don't!)

>> >
>> >> '. Basically, I don't think I get what I'm making response equal to, is
>> >> it an empty string, or?
>> >
>> > != and == are tests and not assignments. If you exchange == for !=
>> > you also need to exchange branches of the if else end construct.
>>
>> So basically with != and == I'm asking if what precedes the expression
>> is either "different from" or "the same as" whatever follows. Am I
>> correct?
>>
>
> That would be correct.
>

Great!

>
>> Also, I still don't think I fully understand the empty string. In the
>> two lines of code...
>>
>> if response != ''
>> responses.push response
>>
>> ...am I doing something like "Is what was entered for the variable
>> response different from an empty string? If so then push whatever string
>> is entered to the end of the responses array."
>>
>
> That would also be correct. One other small item, normally code is
> indented
> (Ruby standard is 2 spaces) another level inside a control statement
> (if/while/else and such) to make it slightly easier to read the code.

I've been trying to do this as well as line up certain elements of code,
but keep slipping in my enthusiasm to get it all out there. Def. need to
try harder to keep it up. What about using Tab to indent, is there any
reason that might be frowned upon? Btw, I'm using TextMate.

>
> Taking your earlier work
>
> if response != ''
> responses.push response
> else
> puts responses.sort
> break
> end
>
>
> It seemed to me that the logical way to enter this code would be
>>
>> responses =
>> response = gets.chomp
>>
>> while response != ''
>> responses.push response
>> end
>>
>> puts responses.sort
>>
>
> Actually very close here. Your problem is that you end up in an endless
> loop because you never get another entry from the user.
>
> This should do the trick
>
> responses =
> response = gets.chomp
>
> while response != ''
> responses.push response
> response = gets.chomp # get another line from the user
> end
>
> puts responses.sort
>

Oh wow, ok, I think I get it. This is super clean too. Much nicer than
what I originally had. Now, one last question, I need response =
gets.chomp in the first instance just to define what the variable stands
for, correct? It's not actually until the second instance where I'm
soliciting input from the user, or actually is it more so the first
instance both defines the variable and solicits input from the user
while the second instance solicits input in a continuous loop until it
gets broken, i.e. when a user enters an empty string?

> But very very close for someone just picking up programming. You appear
> to
> at least grasp the basic mechanics of working through a problem!
>
> Best of luck to you!
>
> John

Thanks again John, super helpful! Almost done with chapter 9, "Writing
Your Own Methods". Only 100 pages to go!

On another note, do you have any recommendations for Ruby resources
and/or a path to take towards Ruby On Rails? I'm planning on finishing
Learning to Program, doing the Rails for Zombies courses, then diving
into Michael Hartl's tutorial for Rails 2.3 and then reading Agile
Development with Rails 3rd edition. Thoughts? Btw, I'm learning 2.3 as a
site I had built was in 2.3.8 and I figure I should learn that first and
then later learn 3 / 3.1.

Thanks,

Emeka

--
Posted via http://www.ruby-forum.com/\.