# Push Inputted Words

**URL:** <https://www.ruby-forum.com/t/push-inputted-words/171224>\
**Category:** Ruby\
**Created:** [June 29, 2009, 6:06am UTC](https://www.ruby-forum.com/t/push-inputted-words/171224 "2009-06-29T06:06:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [June 29, 2009, 6:06am UTC](https://www.ruby-forum.com/t/push-inputted-words/171224/1 "2009-06-29T06:06:48Z")

</div>

I’m trying to make a program that gets you to input words and when you  
press enter with nothing typed it sorts them alphabetically. My code  
right now is:

words=[]  
while words!=’’  
words.push gets  
end  
if words==’’  
words.sort  
end

It runs and asks for words. I assume that it is pushing them into the  
array but it isn’t stopping and sorting them when there is nothing  
inputted by the user. Hope I gave enough information.

As always thanks in advance,  
Scott Andrechek

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [June 29, 2009, 6:59am UTC](https://www.ruby-forum.com/t/push-inputted-words/171224/2 "2009-06-29T06:59:51Z")

</div>

I’m now using this:

words=[]  
word = ‘any’  
while word!= ‘’  
words.push gets  
word= gets.chomp  
end  
puts  
puts words.sort

but as you might notice this code will skip every second word to find a  
matching variable for word :S

-Scott

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [June 29, 2009, 7:33am UTC](https://www.ruby-forum.com/t/push-inputted-words/171224/3 "2009-06-29T07:33:05Z")

</div>

On Sun, Jun 28, 2009 at 9:59 PM, Scott Andrechek  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> but as you might notice this code will skip every second word to find a  
> matching variable for word :S

Try this

> <https://gist.github.com/137478>

John

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [June 29, 2009, 7:52am UTC](https://www.ruby-forum.com/t/push-inputted-words/171224/4 "2009-06-29T07:52:20Z")

</div>

> Try this
> 
> [137478’s gists · GitHub](http://gist.github.com/137478)
> 
> John

It workes great I just don’t what @ does :S [would to know for future  
reference and use : ) ]

-Scott

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [June 29, 2009, 8:02am UTC](https://www.ruby-forum.com/t/push-inputted-words/171224/5 "2009-06-29T08:02:23Z")

</div>

Scott,

On Sun, Jun 28, 2009 at 10:52 PM, Scott Andrechek  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > Try this
> > 
> > [137478’s gists · GitHub](http://gist.github.com/137478)
> > 
> > John
> 
> It workes great I just don’t what @ does :S [would to know for future  
> reference and use : ) ]

Denotes an instance variable. Allows you to set the variable in the  
function  
and still access it within the while loop.

John

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [June 29, 2009, 9:02am UTC](https://www.ruby-forum.com/t/push-inputted-words/171224/6 "2009-06-29T09:02:16Z")

</div>

Or to void instance variables:

words = []  
while (word = gets.chomp) != ‘’  
words.push word  
end  
puts words.sort

sascha

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [June 29, 2009, 11:04am UTC](https://www.ruby-forum.com/t/push-inputted-words/171224/7 "2009-06-29T11:04:39Z")

</div>

Hi,

Am Montag, 29. Jun 2009, 14:52:24 +0900 schrieb Scott Andrechek:

> > Try this
> > 
> > [137478’s gists · GitHub](http://gist.github.com/137478)
> > 
> > John
> 
> It workes great I just don’t what @ does :S [would to know for future  
> reference and use : ) ]

[Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html)

Is it asked too much that you read the first seven paragraphs  
before posting?

Bertram

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [June 29, 2009, 3:57pm UTC](https://www.ruby-forum.com/t/push-inputted-words/171224/8 "2009-06-29T15:57:58Z")

</div>

> Is it asked too much that you read the first seven paragraphs  
> before posting?
> 
> Bertram

Is it too much for you to ignore a thread that you find objectionable?

John
