What am I doing wrong?

Discussion in 'Ruby' started by Ast Jay, Jan 28, 2010.

  1. Ast Jay

    Ast Jay Guest

    I didn't see a beginners forum - hope it's ok to post this here.

    I feel so daft as this must be the simplest question you've probably
    been asked!

    Anyway I'm new to Ruby and want to create a very simple script that
    generates 100 random words and puts them into an array. I want to put
    them into an array so I can use array.uniq! so there are no duplicates.

    Here's my code so far (although I have tried lots of variations
    already).

    ----------------
    def create_word
    first_letter = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n",
    "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"].shuffle[0..6].join
    end

    def create_list(word)
    words = []
    words << word
    end

    100.times do
    list << create_list(create_word)
    puts list
    end

    --------

    Like I said I am a beginner... well that's my excuse lol.

    Hope someone can help!
    --
    Posted via http://www.ruby-forum.com/.
    Ast Jay, Jan 28, 2010
    #1
    1. Advertising

  2. On Thursday 28 January 2010, Ast Jay wrote:
    > |I didn't see a beginners forum - hope it's ok to post this here.
    > |
    > |I feel so daft as this must be the simplest question you've probably
    > |been asked!
    > |
    > |Anyway I'm new to Ruby and want to create a very simple script that
    > |generates 100 random words and puts them into an array. I want to put
    > |them into an array so I can use array.uniq! so there are no duplicates.
    > |
    > |Here's my code so far (although I have tried lots of variations
    > |already).
    > |
    > |----------------
    > |def create_word
    > | first_letter = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n",
    > |"p", "q", "r", "s", "t", "v", "w", "x", "y", "z"].shuffle[0..6].join
    > |end
    > |
    > |def create_list(word)
    > | words = []
    > | words << word
    > |end
    > |
    > |100.times do
    > | list << create_list(create_word)
    > | puts list
    > |end
    > |
    > |--------
    > |
    > |Like I said I am a beginner... well that's my excuse lol.
    > |
    > |Hope someone can help!


    First of all it would be better, when asking for help, to tell exactly what
    the problem is (for example, including the error message you obtain or
    explaining why the result your code produces is not what you expected). This
    way, you make things easier for people wanting to help you.

    As for your code. The error is in the line

    list << create_list(create_word)

    The problem is that something called list has never been defined before, so
    ruby doesn't know what to do with it. What you wanted is

    list = create_list(create_word)

    This creates a local variable called list and stores inside it the value that
    the create_list method returns.

    I hope this helps

    Stefano
    Stefano Crocco, Jan 28, 2010
    #2
    1. Advertising

  3. Ast Jay wrote:
    > I didn't see a beginners forum - hope it's ok to post this here.
    >
    > I feel so daft as this must be the simplest question you've probably
    > been asked!
    >
    > Anyway I'm new to Ruby and want to create a very simple script that
    > generates 100 random words and puts them into an array. I want to put
    > them into an array so I can use array.uniq! so there are no duplicates.
    >


    Hi and welcome to Ruby and its community!
    Your code looks good so far.

    >def create_word
    > first_letter = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n",
    > "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"].shuffle[0..6].join
    > end


    You don't need to create "first_letter". Ruby returns the last
    expression evaluated -- so when you run that line, it returns a string
    of seven characters.

    > def create_list(word)
    > words = []
    > words << word
    > end


    Clever! There is however something even CLEVERER!
    def create_list word # note the parenthesis are optional!
    [word]
    end

    As you can see, there's no need for a method just to do that.

    > 100.times do
    > list << create_list(create_word)
    > puts list
    > end


    A "Set" is an array that doesn't accept duplicates. So you can do this:

    require 'set'
    list = Set.new
    100.times { list << create_word }

    or...
    list = []
    100.times { list << create_word }
    list.uniq!

    Make sense?
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Jan 28, 2010
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    Here's your problem:

    def create_list(word)

    words = []

    words << word

    end


    Every time you call create_list, you make a new list.

    If I were going to write your code, I'd do it like this:

    words = []

    100.times { words << ("a".."z").to_a.shuffle[0..6].join }

    words.uniq!


    Make sense?
    Steve Klabnik, Jan 28, 2010
    #4
  5. Ast Jay

    Josh Cheek Guest

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

    On Thu, Jan 28, 2010 at 2:47 PM, Ast Jay <> wrote:

    > I didn't see a beginners forum - hope it's ok to post this here.
    >
    > I feel so daft as this must be the simplest question you've probably
    > been asked!
    >
    > Anyway I'm new to Ruby and want to create a very simple script that
    > generates 100 random words and puts them into an array. I want to put
    > them into an array so I can use array.uniq! so there are no duplicates.
    >
    > Here's my code so far (although I have tried lots of variations
    > already).
    >
    > ----------------
    > def create_word
    > first_letter = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n",
    > "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"].shuffle[0..6].join
    > end
    >
    > def create_list(word)
    > words = []
    > words << word
    > end
    >
    > 100.times do
    > list << create_list(create_word)
    > puts list
    > end
    >
    > --------
    >
    > Like I said I am a beginner... well that's my excuse lol.
    >
    > Hope someone can help!
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    Don't be so hard on yourself :)

    I'm not completely certain what you were wanting to happen, this works for
    me, and seems to behave in pretty much the same way your looked like it
    wanted to behave.

    def create_word
    Array('b'..'z').shuffle[0..6].join
    end

    list = Array.new
    100.times { list << create_word }

    puts list
    Josh Cheek, Jan 28, 2010
    #5
  6. Ast Jay

    Ast Jay Guest

    Wow! Thank you all for such a fast response - I am shocked as I wasn't
    expecting replies so soon!

    Also apologies as I forgot to take out the 'first_letter' variable. The
    code was actually part of a bigger block to create a word based on
    exactly what I want - a first_letter, middle_letters, and an end_letter.
    I just tried to keep it simple when I posted here and forgot to clean
    that bit out.

    BIG thank you for all that helped - it looks easy when someone else
    gives you the code :) I ended up using Aldric's code:

    -------------------
    def create_word
    # code block here to construct a random word to my exact spec
    end

    list = []
    1000.times { list << create_word }
    list.uniq!

    puts list
    ------------------

    Once again a huge thank you - the Ruby community really rocks! I also
    appreciate the kind words of encouragement - I really did feel stupid
    posting this as the answer just seemed so obvious *blush*.

    I just hope I am as good as you guys one day!

    --
    Posted via http://www.ruby-forum.com/.
    Ast Jay, Jan 28, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Salisha Khan
    Replies:
    1
    Views:
    11,645
    JohnFol
    Aug 1, 2003
  2. Keith R. Williams

    What am I doing wrong?

    Keith R. Williams, Jul 14, 2003, in forum: VHDL
    Replies:
    4
    Views:
    1,038
    Ed Diego
    Jul 15, 2003
  3. ed
    Replies:
    1
    Views:
    655
  4. Aaron Ackerman
    Replies:
    2
    Views:
    370
    Lewis Wang [MSFT]
    Jul 29, 2003
  5. J

    What am I doing wrong

    J, Aug 14, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    381
    John Saunders
    Aug 14, 2003
Loading...

Share This Page