# New to Classes

**URL:** <https://www.ruby-forum.com/t/new-to-classes/174123>\
**Category:** Ruby\
**Created:** [August 21, 2009, 3:19am UTC](https://www.ruby-forum.com/t/new-to-classes/174123 "2009-08-21T03:19:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [August 21, 2009, 3:19am UTC](https://www.ruby-forum.com/t/new-to-classes/174123/1 "2009-08-21T03:19:16Z")

</div>

Hello. I’m new to making and defining your own classes (well really to  
Ruby in general)and need some of your help with areas of my code. Here  
is the code for the game:  
class Die

def roll  
@numberShowing = 1 + rand(6)  
end

def showing  
@numberShowing  
end

end

dice = [Die.new]  
play=true  
money= 100  
while play==true  
puts ‘You have $’ + money.to\_s + ‘. What is your bid?’  
bid= gets.chomp  
puts ‘$’ + money.to\_s + ‘-’ + ‘$’ + bid.to\_s  
moneytest= money.to\_i - bid.to\_i  
while moneytest\<=0  
puts ‘Please enter a valid bid’  
bid=gets.chomp  
moneytest= money.to\_i - bid.to\_i  
end  
money=(money.to\_i - bid.to\_i)  
puts ‘$’ + money.to\_s + ’ remaining’  
puts ‘What number would you like to place your bet of ’ + ‘$’ + bid + ’  
on?’  
number= gets.chomp  
puts ‘Dice roll was…’  
die = Die.new  
die.roll  
puts die.showing  
if @numberShowing.to\_i==number.to\_i  
puts 'Congrats! You made ’ + ‘$’ + bid.to\_s  
else  
puts 'Sorry. You lost ’ + ‘$’ + bid.to\_s  
end  
if (money==0 or money\<=0)  
puts ‘Sorry you lose’  
play==false  
end  
end

The problems I’m having with it (as you will see if you test it) is that  
you cannont win money, which I assume is because of my handling of  
@numberShowing, and that you can’t go ‘all in’. Any help would be  
greatly appreciated.

-Scott

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [August 21, 2009, 4:15am UTC](https://www.ruby-forum.com/t/new-to-classes/174123/2 "2009-08-21T04:15:56Z")

</div>

Scott Andrechek wrote:

> ```
> @numberShowing
> 
> ```
> 
> puts ‘$’ + money.to\_s + ‘-’ + ‘$’ + bid.to\_s  
> number= gets.chomp  
> puts ‘Dice roll was…’  
> die = Die.new  
> die.roll  
> puts die.showing  
> if @numberShowing.to\_i==number.to\_i

Why are you using @numberShowing instead of die.showing? If you print  
@numberShowing here I think you will see your problem.

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [August 21, 2009, 8:57am UTC](https://www.ruby-forum.com/t/new-to-classes/174123/3 "2009-08-21T08:57:48Z")

</div>

Michael W. Ryder wrote:

> Scott Andrechek wrote:
> 
> > ```
> > @numberShowing
> > 
> > ```
> > 
> > puts ‘$’ + money.to\_s + ‘-’ + ‘$’ + bid.to\_s  
> > number= gets.chomp  
> > puts ‘Dice roll was…’  
> > die = Die.new  
> > die.roll  
> > puts die.showing  
> > if @numberShowing.to\_i==number.to\_i
> 
> Why are you using @numberShowing instead of die.showing? If you print  
> @numberShowing here I think you will see your problem.

Thanks. You can now win money. Unfortanatley you still can’t go ‘all  
in’. Hopefully a solution will come up…

-Scott

---

<div class="post-metadata">

**Author:** ![ss\_1982](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@ss\_1982](https://www.ruby-forum.com/u/ss_1982)\
**Post date:** [August 21, 2009, 3:42pm UTC](https://www.ruby-forum.com/t/new-to-classes/174123/4 "2009-08-21T15:42:03Z")

</div>

On Fri, Aug 21, 2009 at 2:57 AM, Scott  
Andrechek[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Unfortanatley you still can’t go ‘all  
> in’. Hopefully a solution will come up…

Try changing

while moneytest\<=0

to

while moneytest\<0

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
