Well, I can’t believe that I’m back already with another question.
Don’t fret, only a few more chapters in this book to go
My task is to loop through a “gets” and end the program by just doing
an empty, no input, just enter. The purpose is too add x elements to
an array. Problem though is the print out is showing only the last
element entered. I know from looking at other languages there was
usually an incrementer. Still, using .push I’d expect the slots to
continue to fill up, maybe a wrong perception.
$stdout.sync = true
myarray = []
another = gets.chomp
while gets.chomp != ‘’
another = gets.chomp
myarray.push(another)
end
This is similar to the problem you were having earlier. You have to
be very careful about the order in which you do things. Inside your
loop, you are asking for another value before you push the current one
into the array. You could make that work, but it’s probably easier if
you switch the two.
Also, you are calling gets too many times. Remember, every time you
do that the user will need to input a new string
while gets.chomp != ‘’
In your code here you are asking for a value. You check it correctly,
but what happens to the value? You aren’t binding it to anything, so
the code looses that string. Make sure you’re assigning everything
somewhere with =
another = gets.chomp
myarray.push(another)
end
Here’s what I was talking about above. You are asking for a new value
before you store the old one. That might be okay, but not if you
prime the loop input as you’ve done. This will make you loose your
first value also.
In the while condition, you’ve already got a value, which you stored
in the variable “another”. So just check that for an empty string:
while another != ''
Then switch your two lines so that you store the good value you just
checked for:
myarray.push(another)
another = gets.chomp
end
Then you should be good to go. Keep at it, and happy ruby hacking
Now that I understand that, then why do I need the
“another = gets.chomp” after the storing of the element. Shouldn’t
the while statement take care of it, and I already know it doesn’t
work, but since it’s a loop I’m just confused as to the why it doesn’t
work.
Because if you look at the code again, I took the “gets” out of the
while condition. It’s just checking to see what is in the “another”
variable. That means you manually have to go and grab the next one
before you go to the next iteration.
I haven’t read the book, so I don’t know if you’ve learned that, in
ruby, assignments can be used as an expression. That is to say they
return values. Practically that means that you can do this:
while (another = gets.chomp) != ''
...
# and do stuff with "another"
So here’s the run-down: “gets” reads a string, and that value is
chomped. It then is bound to your local variable “another”. At this
point, the = itself returns the value it on it’s right hand side,
which is the string you read in. You check that for the null string
and go from there.
Also, you are calling gets too many times. Remember, every time you
do that the user will need to input a new string
while gets.chomp != ‘’
Yep, I was focusing on looping that I forgot it was also asking for
input
Which is why i had the “not needed” “another = gets.chomp” prior to
storing the value.
Now that I understand that, then why do I need the
“another = gets.chomp” after the storing of the element. Shouldn’t
the while statement take care of it, and I already know it doesn’t
work, but since it’s a loop I’m just confused as to the why it doesn’t
work.
myarray.push(another)
another = gets.chomp
end
Lou.
Stuart
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.