# Queston: array.push

**URL:** <https://www.ruby-forum.com/t/queston-array-push/96044>\
**Category:** Ruby\
**Created:** [June 23, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/queston-array-push/96044 "2006-06-23T20:28:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [June 23, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/queston-array-push/96044/1 "2006-06-23T20:28:53Z")

</div>

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

myarray.each do |mine|  
puts mine  
end

TIA  
Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [June 23, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/queston-array-push/96044/2 "2006-06-23T20:38:17Z")

</div>

Problem is you’re calling gets way too much.

compare this working code to your own:

$stdout.sync = true

myarray = []

while (another = gets.chomp)  
break if another.empty?  
myarray.push(another)  
print another.inspect  
end

print myarray.inspect

myarray.each do |mine|  
puts mine  
end

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [June 23, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/queston-array-push/96044/3 "2006-06-23T20:47:55Z")

</div>

Thanks Jeffrey,  
I hadn’t been told about break yet. I know if / else would have worked  
in it’s place.

Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [June 23, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/queston-array-push/96044/4 "2006-06-23T20:48:04Z")

</div>

Hi Stuart;

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 🙂

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [June 23, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/queston-array-push/96044/5 "2006-06-23T22:25:46Z")

</div>

> 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.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [June 23, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/queston-array-push/96044/6 "2006-06-23T21:16:39Z")

</div>

On 6/23/06, Louis J Scoras [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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
