# Simple Script Help (New to Ruby)

**URL:** <https://www.ruby-forum.com/t/simple-script-help-new-to-ruby/54436>\
**Category:** Ruby\
**Created:** [March 5, 2006, 12:37am UTC](https://www.ruby-forum.com/t/simple-script-help-new-to-ruby/54436 "2006-03-05T00:37:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmckible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@jmckible](https://www.ruby-forum.com/u/jmckible)\
**Post date:** [March 5, 2006, 12:37am UTC](https://www.ruby-forum.com/t/simple-script-help-new-to-ruby/54436/1 "2006-03-05T00:37:24Z")

</div>

Hello Ruby Masters!

I’m attempting to write a very simple guessing game script in an effort  
to expand my knowledge about the Ruby Language. I’m running ruby on my  
OpenSuse Linux 10 box at home. I’m typically a web-programmer, but I  
want to get into something a little more deep.

Here is my little script:

* * *

# set the default result

theResult = 0

# create the random number

theNumber = rand(10)

while theResult != 3  
print "Guess my number: "  
theGuess = gets.to\_i

```
    # return key
    # 1 = number is lower
    # 2 = number is higher
    # 3 = number matched!
    if theNumber < theGuess
            theResult = 1
    elseif theNumber > theGuess
            theResult = 2
    elseif theNumber = theGuess
            theResult = 3
    end

    case theResult
            when 1
                    puts "My number is lower."
            when 2
                    puts "My number is higher."
            when 3
                    puts "You guessed my number!"
    end

    print "The Result: "
    print theResult
    print "\n"

    print "The Guess: "
    print theGuess.class
    print "+"
    print theGuess
    print "\n"

    print "The Number: "
    print theNumber
    print "\n"

```

## end

and here’s my output when I run it:

* * *

## utdream@dream:~/ruby\_scripts\> ruby guess\_number.rb Guess my number: 1 The Result: 0 The Guess: Fixnum+1 The Number: 3

The result staying at 0 tells me that NONE of my comparisons worked. Can  
anyone tell me why? I’m sure this is something simple but any help would  
be appreciated.

Thanks in advance!

Warm regards,  
Jordan M.  
Vivio Technologies

---

<div class="post-metadata">

**Author:** ![jmckible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@jmckible](https://www.ruby-forum.com/u/jmckible)\
**Post date:** [March 5, 2006, 1:04am UTC](https://www.ruby-forum.com/t/simple-script-help-new-to-ruby/54436/2 "2006-03-05T01:04:52Z")

</div>

> ```
> elseif theNumber = theGuess
> 
> ```

I guess you mean == here, not =.

Best, Ulrich

---

<div class="post-metadata">

**Author:** ![jmckible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@jmckible](https://www.ruby-forum.com/u/jmckible)\
**Post date:** [March 5, 2006, 1:04am UTC](https://www.ruby-forum.com/t/simple-script-help-new-to-ruby/54436/3 "2006-03-05T01:04:53Z")

</div>

Jordan M. wrote:

> ```
> end
> print "The Result: "
> print theNumber
> print "\n"
> 
> ```
> 
> ## end

(there is no elseif kayword but elsif)

It seems to be working for me. You could replace the if/elsif statements  
with case statement

case  
when theNumber \< theGuess  
# blah  
when theNumber \> theGuess  
# blah  
else  
# blah  
end

lopex

---

<div class="post-metadata">

**Author:** ![jmckible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@jmckible](https://www.ruby-forum.com/u/jmckible)\
**Post date:** [March 5, 2006, 1:07am UTC](https://www.ruby-forum.com/t/simple-script-help-new-to-ruby/54436/4 "2006-03-05T01:07:54Z")

</div>

Jordan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> ```
> elseif theNumber > theGuess
> theResult = 2
> elseif theNumber = theGuess
> 
> ```

elseif is not Ruby… try elsif instead.

furthermore, equality is “==”, not “=”. Here, you assigns theGuess to  
theNumber. This assignment return the value of theGuess, hence always  
“true”.

> and here’s my output when I run it:

Strange. If i try to run your script, i get a :

bla.rb:17: undefined method `elseif’ for main:Object (NoMethodError)

That’s not the case for you ?

---

<div class="post-metadata">

**Author:** ![jmckible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@jmckible](https://www.ruby-forum.com/u/jmckible)\
**Post date:** [March 5, 2006, 1:07am UTC](https://www.ruby-forum.com/t/simple-script-help-new-to-ruby/54436/5 "2006-03-05T01:07:54Z")

</div>

oops it seems I missed this (theNumber = theGuess)

lopex

---

<div class="post-metadata">

**Author:** ![jmckible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@jmckible](https://www.ruby-forum.com/u/jmckible)\
**Post date:** [March 5, 2006, 1:16am UTC](https://www.ruby-forum.com/t/simple-script-help-new-to-ruby/54436/6 "2006-03-05T01:16:58Z")

</div>

Eric J. wrote:

> > 
> 
> Strange. If i try to run your script, i get a :
> 
> bla.rb:17: undefined method `elseif’ for main:Object (NoMethodError)
> 
> That’s not the case for you ?

No, it was not the case, but that might have been helpful!

> ruby --version  
> ruby 1.8.2 (2004-12-25) [i586-linux]

I’m downloading 1.8.4 as I write this. We’ll see if that doesn’t give me  
better error messages.

Thanks to all for your support and suggestions! Changing the “elseif” to  
“elsif” did the trick. The script now works as intended. I agree with a  
lot of your suggestions about equality (==), and using case instead of  
if. When programming for clients I make a point to use the most  
efficient and correct code as possible, but I’m just playing with this  
at the moment. 😉

Thanks again. It’s nice to have a friendly place to turn to when you  
have questions!

Warm regards,  
Jordan M.  
Vivio Technologies

---

<div class="post-metadata">

**Author:** ![jmckible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@jmckible](https://www.ruby-forum.com/u/jmckible)\
**Post date:** [March 5, 2006, 2:02am UTC](https://www.ruby-forum.com/t/simple-script-help-new-to-ruby/54436/7 "2006-03-05T02:02:19Z")

</div>

On 3/4/06, Jordan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # set the default result
> 
> ```
> # 1 = number is lower
> case theResult
> when 1
> puts "My number is lower."
> when 2
> puts "My number is higher."
> when 3
> puts "You guessed my number!"
> end
> 
> ```

This is a good place to use symbols – if nothing else, you won’t need  
the comments explaining the keys:

```
     if theNumber < theGuess
             theResult = :lower
     elsif theNumber > theGuess
             theResult = :higher
     elsif theNumber = theGuess
             theResult = :equal
     end

     case theResult
             when :lower
                     puts "My number is lower."
             when :higher
                     puts "My number is higher."
             when :equal
                     puts "You guessed my number!"
     end

```

---

<div class="post-metadata">

**Author:** ![jmckible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c4cdca/32.png) [@jmckible](https://www.ruby-forum.com/u/jmckible)\
**Post date:** [March 5, 2006, 2:29am UTC](https://www.ruby-forum.com/t/simple-script-help-new-to-ruby/54436/8 "2006-03-05T02:29:32Z")

</div>

On 4-Mar-06, at 8:00 PM, Bill G. wrote:

> > Here is my little script:  
> > theGuess = gets.to\_i  
> > theResult = 3
> 
> > ```
> > print "The Result: "
> > print theNumber
> > 
> > ```
> > 
> > ## The Guess: Fixnum+1 The Number: 3
> > 
> > The result staying at 0 tells me that NONE of my comparisons  
> > worked. Can  
> > anyone tell me why? I’m sure this is something simple but any help  
> > would  
> > be appreciated.

You could avoid the symbols if you are prepared to understand the \<=\>  
comparison, a short example might be

the\_number = rand(10)

loop do  
print "Guess my number: "  
the\_guess = gets or break # hit eof

case the\_number \<=\> the\_guess.to\_i  
when -1  
puts “My number is lower”  
when 1  
puts “My number is higher”  
else  
puts “You guessed my number”  
break  
end  
end

It may be interesting to see how you can trap “bad” input and  
generate a useful error message using what comes with Ruby

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.
