# Re: Word Blender (#108)

**URL:** <https://www.ruby-forum.com/t/re-word-blender-108/225937>\
**Category:** Ruby\
**Created:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/re-word-blender-108/225937 "2007-01-19T16:31:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/re-word-blender-108/225937/1 "2007-01-19T16:31:33Z")

</div>

Hi,

I’m new to ruby and this is my first quiz entry, so  
feel free to offer any feedback/suggestions etc.

Requires file english.txt (dicttionary) to be in same  
dir as source.

Play is very basic as I’ve had to fit this in around  
four young kids…  
Enter a six letter word to progress to the next round  
(another six letters), or as many smaller words as you  
like to stay in the same round.

Cheers,  
Dave

* * *

?class Game

def initialize  
@short\_words= []  
@long\_words= []  
get\_dict  
end

def get\_dict  
puts “Reading File…”  
File.open(“english.txt”,“r”).each\_line do |line|  
line.chomp!  
@short\_words \<\< line.downcase if  
(3…5).include?(line.length)  
@long\_words \<\< line.downcase if line.length== 6  
end  
puts "Short words: " + @short\_words.length.to\_s  
puts "Six letter words: " +  
@long\_words.length.to\_s  
end

def start  
round\_number= 0  
begin  
create\_targets  
end while play\_round(round\_number+= 1)  
end

def create\_targets  
@target\_word=  
@long\_words[rand(@long\_words.length)]  
@target\_letters= @target\_word.clone  
10.times do  
pos= rand(6)  
@target\_letters[0,1], @target\_letters[pos,1]=  
@target\_letters[pos,1], @target\_letters[0,1]  
end  
end

def letter\_match?(guess, target)  
0.upto(guess.length-1) do |i|  
letter= guess[i,1]  
if target.include?(letter) then  
target[letter]=“”  
else return false  
end  
end  
return true  
end

def get\_score(words\_guessed, guess, target)  
points, round\_end= 0, false  
if @short\_words.include?(guess) ||  
@long\_words.include?(guess)  
if words\_guessed.include?(guess)  
puts “You’ve used that word. No points this  
time.”  
else  
if letter\_match?(guess, target)  
words\_guessed \<\< guess  
if guess.length==6  
puts “\*\*\* Well Done! You got the big one!  
\*\*\*”  
points= 1  
round\_end= true  
else  
puts “Well done (+1 point)”  
points= 1  
end  
else puts “I know that word, but it doesn’t  
use the right letters. No points.”  
end  
end  
else puts “I don’t know that word”  
end  
return points, round\_end  
end

def play\_round(round\_number)  
words\_guessed= []  
quit= false  
puts “\n\n—Round Number #{round\_number}—”  
puts “\nLetters are: #{@target\_letters.upcase}”  
begin  
print “\n[#{@target\_letters.upcase}]  
Points:#{words\_guessed.length}. Guess (uit)\>”  
guess= gets.downcase.chomp  
quit= (guess== ‘q’)  
if quit then puts “Bye”  
else points, round\_end= get\_score(words\_guessed,  
guess, @target\_letters.clone)  
end  
end while not (round\_end || quit)

```
puts "You guessed #{words_guessed.sort.join(",

```

“)}\nWord was #{@target\_word}” if !quit  
return !quit  
end #play\_round

end #class

game= Game.new  
game.start

* * *

What kind of emailer are you? Find out today - get a free analysis of  
your email personality. Take the quiz at the Yahoo! Mail Championship.  
[http://uk.rd.yahoo.com/evt=44106/\*http://mail.yahoo.net/uk](http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk)

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [September 25, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/re-word-blender-108/225937/2 "2007-09-25T22:32:51Z")

</div>

On Jan 11, 2007, at 3:37 PM, David and Sharon P. wrote:

> I’m new to ruby and this is my first quiz entry, so  
> feel free to offer any feedback/suggestions etc.

I like it. Looks nice and clean. My only suggestion would be to  
prefer string interpolation to addition. For example:

"Short words: " + @short\_words.length.to\_s

Is simpler as:

“Short words: #{@short\_words.length}”

Saves some to\_s() calls at the very least.

James Edward G. II
