# Beginner Needs Help

**URL:** <https://www.ruby-forum.com/t/beginner-needs-help/76247>\
**Category:** Ruby\
**Created:** [November 16, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/beginner-needs-help/76247 "2006-11-16T22:58:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 16, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/beginner-needs-help/76247/1 "2006-11-16T22:58:00Z")

</div>

I am new to Ruby and OOP although I have written programs in Forth;  
another  
world. As a learning project, I decided to duplicate something I had  
written  
in Forth; namely a bridge hand dealer. I originally stored the hands in  
a  
3-dimensional binary array (hand, suit, cards). In cards, a set bit  
represented  
a particular card. I like the binary representation because it makes  
things  
like searching for pairs or sequences, etc. easy with bit logic.

Here is what I have written so far:

class Carddeck  
attr\_writer :deck  
def initialize  
@deck=Array.new  
end

def build\_deck  
0.upto(51) {|i| @deck.push(i.divmod(13))}

# deck consists of 52 2-member arrays, [suit, rank],

# each representing a playing card. In use, the first

# 13 members is hand[0] and the next 13 are hand[1], etc.

# hand will be defined in a subclass.

return @deck  
end

def shuffle\_deck  
@deck=@deck.sort\_by{rand}  
end  
end

class Bridgehands \< Carddeck  
attr\_writer :hands  
attr\_reader :deck  
def initialize  
super  
@hands = Array.new(4) {|idx| Array.new(4)}

# hands is a 4 by 4 array for player,suit

end

def build\_hands  
self.shuffle\_deck  
0.upto(51) {|i| @hands[i/13][@deck[i][0]] += 0b1 \<\< @deck[i][1]}

# The left side addresses a particular cell in hands.

# The right side converts the card rank to a bit shifted

# to the position represented by the rank of the card.

# We cycle through the entire deck and let i/13 = the hand.

return @hands  
end  
end

Carddeck works just fine but Bridgehands returns an error when I enter:

bh = Bridgehands.new followed by bh.build\_deck and then bh.build\_hands.  
to  
which it responds:

NoMethodError: undefined method `+' for nil:NilClass from (irb):92:in`build\_hands’  
from (irb):92:in `build\_hands’  
from (irb):103  
from :0

I’m going nuts.

Charles G. – Phoenix, AZ … skype and google-talk user name:  
tcykgreis

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 16, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/beginner-needs-help/76247/2 "2006-11-16T23:16:07Z")

</div>

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> def build\_deck  
> 0.upto(51) {|i| @deck.push(i.divmod(13))}
> 
> # deck consists of 52 2-member arrays, [suit, rank],
> 
> # each representing a playing card. In use, the first
> 
> # 13 members is hand[0] and the next 13 are hand[1], etc.
> 
> # hand will be defined in a subclass.
> 
> return @deck  
> end  
> […]  
> def initialize  
> super  
> @hands = Array.new(4) {|idx| Array.new(4)}
> 
> # hands is a 4 by 4 array for player,suit
> 
> end  
> […]  
> def build\_hands  
> self.shuffle\_deck  
> 0.upto(51) {|i| @hands[i/13][@deck[i][0]] += 0b1 \<\< @deck[i][1]}

IRB shows @hands looks like:

irb(main):024:0\> hands = Array.new(4) {|idx| Array.new(4)}  
=\> [[nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, nil],  
[nil, nil, nil, nil]]

So,

@hands[0][@deck[0][0]]

is:

@hands[0][0]

which is fetching the first ‘nil’ shown above in IRB.

Regards,

Bill

P.S. FORTH LOVE IF 7 EMIT THEN
