Push Inputted Words

Discussion in 'Ruby' started by Scott Andrechek, Jun 29, 2009.

  1. 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
     
    Scott Andrechek, Jun 29, 2009
    #1
    1. Advertisements

  2. 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
     
    Scott Andrechek, Jun 29, 2009
    #2
    1. Advertisements

  3. [Note: parts of this message were removed to make it a legal post.]

    Try this

    http://gist.github.com/137478

    John
     
    John W Higgins, Jun 29, 2009
    #3
  4. Try this
    It workes great I just don't what @ does :S [would to know for future
    reference and use : ) ]

    -Scott
     
    Scott Andrechek, Jun 29, 2009
    #4
  5. [Note: parts of this message were removed to make it a legal post.]

    Scott,


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

    John
     
    John W Higgins, Jun 29, 2009
    #5
  6. Scott Andrechek

    Sascha Abel Guest

    Or to void instance variables:

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

    sascha
     
    Sascha Abel, Jun 29, 2009
    #6
  7. Bertram Scharpf, Jun 29, 2009
    #7
  8. [Note: parts of this message were removed to make it a legal post.]
    Is it too much for you to ignore a thread that you find objectionable?

    John
     
    John W Higgins, Jun 29, 2009
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.