# What am I doing wrong?

**URL:** <https://www.ruby-forum.com/t/what-am-i-doing-wrong/182488>\
**Category:** Ruby\
**Created:** [January 28, 2010, 9:47pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/182488 "2010-01-28T21:47:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@ast](https://www.ruby-forum.com/u/ast)\
**Post date:** [January 28, 2010, 9:47pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/182488/1 "2010-01-28T21:47:35Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@ast](https://www.ruby-forum.com/u/ast)\
**Post date:** [January 28, 2010, 10:01pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/182488/2 "2010-01-28T22:01:40Z")

</div>

Ast J. 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?

---

<div class="post-metadata">

**Author:** ![ast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@ast](https://www.ruby-forum.com/u/ast)\
**Post date:** [January 28, 2010, 10:01pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/182488/3 "2010-01-28T22:01:51Z")

</div>

On Thursday 28 January 2010, Ast J. wrote:

> |already).  
> |end  
> |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

---

<div class="post-metadata">

**Author:** ![ast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@ast](https://www.ruby-forum.com/u/ast)\
**Post date:** [January 28, 2010, 10:05pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/182488/4 "2010-01-28T22:05:21Z")

</div>

On Thu, Jan 28, 2010 at 2:47 PM, Ast J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## already). end Hope someone can help!
> 
> Posted via [http://www.ruby-forum.com/](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

---

<div class="post-metadata">

**Author:** ![ast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@ast](https://www.ruby-forum.com/u/ast)\
**Post date:** [January 28, 2010, 10:03pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/182488/5 "2010-01-28T22:03:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@ast](https://www.ruby-forum.com/u/ast)\
**Post date:** [January 28, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/what-am-i-doing-wrong/182488/6 "2010-01-28T22:54:37Z")

</div>

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!
