# \[SUMMARY\] Texas Hold'Em (#24)

**URL:** <https://rubytalk.org/t/summary-texas-holdem-24/17153>\
**Category:** ruby-talk\
**Created:** [24 March 2005 14:04 UTC](https://rubytalk.org/t/summary-texas-holdem-24/17153 "2005-03-24T14:04:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [24 March 2005 14:04 UTC](https://rubytalk.org/t/summary-texas-holdem-24/17153/1 "2005-03-24T14:04:08Z")

</div>

People wrote quite a bit of code to solve this quiz. I don't think it's all  
that tough, but there are quite a few combinations to check for, which seemed to  
increase the line count of the solutions.

There was something interesting in all the solutions though, so I do recommend  
browsing through them if you haven't already. I know I'm always saying that. I  
guess it's always true.

I'm going to show Patrick Hurley's solution below. Patrick resubmitted just to  
defend against my rant about how programs should stay within an 80 character  
line limit. My argument wasn't meant as an attack on any submissions, but I  
still appreciate Patrick's efforts. Here's the start of the code:

&nbsp;&nbsp;#!ruby -w

&nbsp;&nbsp;class Card  
&nbsp;&nbsp;&nbsp;&nbsp;SUITS = "cdhs"  
&nbsp;&nbsp;&nbsp;&nbsp;FACES = "L23456789TJQKA"  
&nbsp;&nbsp;&nbsp;&nbsp;SUIT\_LOOKUP = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'c' =\> 0,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'d' =\> 1,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'h' =\> 2,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'s' =\> 3,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'C' =\> 0,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'D' =\> 1,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'H' =\> 2,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'S' =\> 3,  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;FACE\_VALUES = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'L' =\> 1, # this is a magic low ace  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'2' =\> 2,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'3' =\> 3,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'4' =\> 4,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'5' =\> 5,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'6' =\> 6,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'7' =\> 7,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'8' =\> 8,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'9' =\> 9,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'T' =\> 10,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'J' =\> 11,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Q' =\> 12,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'K' =\> 13,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'A' =\> 14,  
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;def Card.face\_value(face)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (face)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FACE\_VALUES[face] - 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def build\_from\_string(card)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;build\_from\_face\_suit(card[0,1], card[1,1])  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def build\_from\_value(value)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@value = value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@suit = value / FACES.size()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@face = (value % FACES.size())  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def build\_from\_face\_suit(face, suit)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@face = Card::face\_value(face)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@suit = SUIT\_LOOKUP[suit]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@value = (@suit \* FACES.size()) + (@face - 1)  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def build\_from\_face\_suit\_values(face, suit)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;build\_from\_value((face - 1) + (suit \* FACES.size()))  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;# got a little carried away with this constructor 😉  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize(\*value)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (value.size == 1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (value[0].respond\_to?(:to\_str))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;build\_from\_string(value[0])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif (value[0].respond\_to?(:to\_int))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;build\_from\_value(value[0])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif (value.size == 2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (value[0].respond\_to?(:to\_str) &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value[1].respond\_to?(:to\_str))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;build\_from\_face\_suit(value[0], value[1])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif (value[0].respond\_to?(:to\_int) &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value[1].respond\_to?(:to\_int))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;build\_from\_face\_suit\_values(value[0], value[1])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :suit, :face, :value

&nbsp;&nbsp;&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FACES[@face].chr + SUITS[@suit].chr  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;# ...

That's the Card class Patrick uses for tracking individual cards. It looks like  
a lot of code, but it's mostly a single constructor that accepts many different  
forms of initialization. initialize() breaks down the parameters and hands them  
off to the various build\_from\_... methods. Those build methods should probably  
be private, leaning on initialize() as their interface. Once you get past  
construction, you'll see that Card just contains a suit, face, and value.  
Glance at build\_from\_face\_suit() to see how those break down.

You can see it above and a little more below, but this code has a little  
creeping featurism. Patrick was clearly building for the future with the card  
handling classes. That's probably a safe bet as card quizzes are fairly common.  
Dave Burt reused code from his Blackjack solution this time around. All I'm  
saying is, don't be surprised if you see a handful of things in here that never  
get used. Agile purists bare with us...

Let's move on to Deck objects:

&nbsp;&nbsp;# ...

&nbsp;&nbsp;class Deck  
&nbsp;&nbsp;&nbsp;&nbsp;def shuffle  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deck\_size = @cards.size  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(deck\_size \* 2).times do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pos1, pos2 = rand(deck\_size), rand(deck\_size)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@cards[pos1], @cards[pos2] = @cards[pos2], @cards[pos1]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@cards = []  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::SUITS.each\_byte do |suit|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# careful not to double include the aces...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::FACES[1..-1].each\_byte do |face|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@cards.push(Card.new(face.chr, suit.chr))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shuffle()  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def deal  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@cards.pop  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@cards.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;# ...

initialize() just creates and shuffles a deck. deal() pops a card and empty?()  
tells you if there are any left. If you read shuffle(), you'll see that it's  
just a bunch of random swaps. Not sure why Patrick went this way. I believe  
the standard Ruby shuffling idiom is:

&nbsp;&nbsp;@cards.sort\_by { rand }

On to the Hand class, but let's take this one in slices:

&nbsp;&nbsp;# ...

&nbsp;&nbsp;class Hand  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize(cards = [])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (cards.respond\_to?(:to\_str))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hand = cards.scan(/\S\S/).map { |str| Card.new(str) }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hand = cards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :hand

&nbsp;&nbsp;&nbsp;&nbsp;# ...

initialize() just builds new Hand objects from the lines of input in the quiz by  
scan()ing for the two character format. You can also build a Hand from an Array  
of Card objects. Then there's the accessor to get them back.

&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def face\_values  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hand.map { |c| c.face }  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def by\_suit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hand.new(@hand.sort\_by { |c| [c.suit, c.face] }.reverse)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def by\_face  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hand.new(@hand.sort\_by { |c| [c.face, c.suit] }.reverse)  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;# ...

You can use the above methods to request hands by face\_values(), by\_suit(), or  
by\_face(). Note that both of the by\_... sorts also sort by the other value, as a  
secondary condition.

&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def =~ (re)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;re.match(@hand.join(' '))  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def arrange\_hand(md)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hand = if (md.respond\_to?(:to\_str))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;md  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;md[0] + ' ' + md.pre\_match + md.post\_match  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hand.gsub!(/\s+/, ' ')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hand.gsub(/\s+$/,'')  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;# ...

The first method here is an operator overload to allow using regular expressions  
on Hand objects. The second method returns a hand string in an order specified  
by a MatchData object (the else clause). Whatever cards were matched are put  
first, follow by cards preceding the match, and finally trailing cards. This  
floats a matched "hand" to the front of the string while keeping the ordering  
for any non-matched cards. arrange\_hand() can also be called with a string  
order (the if clause), but it doesn't do much in these cases except clean up  
spacing issues.

From here, we start to get into hand matching code:

&nbsp;&nbsp;&nbsp;&nbsp;# ..  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def royal\_flush?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (md = (by\_suit =~ /A(.) K\1 Q\1 J\1 T\1/))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[10], arrange\_hand(md)]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;# ...

This method looks for the coveted royal flush. First it calls by\_suit() to  
order the cards. Remember that will order suits first, then faces. That makes  
it trivial to spot the pattern with a Regexp. When found, royal\_flush?()  
returns a hand ranking number and the properly arranged hand in an Array, which  
is of course a true value in Ruby. false is used when no match is found.

The code then pauses to define a couple more helper methods for spotting the  
other hands:

&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def delta\_transform(use\_suit = false)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aces = @hand.select { |c| c.face == Card::face\_value('A') }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aces.map! { |c| Card.new(1,c.suit) }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;base = if (use\_suit)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(@hand + aces).sort\_by { |c| [c.suit, c.face] }.reverse  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(@hand + aces).sort\_by { |c| [c.face, c.suit] }.reverse  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = base.inject(['',nil]) do |(delta\_hand, prev\_card), card|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (prev\_card)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delta = prev\_card - card.face  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delta = 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# does not really matter for my needs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delta = 'x' if (delta \> 9 || delta \< 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delta\_hand += delta.to\_s + card.to\_s + ' '  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[delta\_hand, card.face]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# we just want the delta transform, not the last cards face too  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result[0].chop  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;# ...

Dave Burt asked on Ruby Talk what delta\_transform() does. Here's the author's  
own response:

&nbsp;&nbsp;The delta transform creates a version of the cards where the delta  
&nbsp;&nbsp;between card values is in the string, so a regexp can then match a  
&nbsp;&nbsp;straight and/or straight flush - I used regexp to match all my cases  
&nbsp;&nbsp;with appropriate sort and/or transforms.

Because that's probably easier to understand when you see it, here's a typical  
return value from delta\_tranform():

&nbsp;&nbsp;"0Jh 38h xJd 38d 44d 13d x8c"

The extra character preceding each card shows the drop from the previous card  
rank. The jack is the first card, so it shows a 0 drop. The eight is then down  
3, as shown. Tracking increases isn't needed in the solution, so the code just  
punts with an x character, as seen with the next jack. All this is just  
building up a handy string for pattern matching.

Note that the first couple of lines of delta\_transform() add a "low ace" to the  
back of the hand for each ace found in the hand. This is for spotting low  
straights, but the magic must eventually be undone by:

&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def fix\_low\_ace\_display(arranged\_hand)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# remove card deltas (this routine is only used for straights)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand.gsub!(/\S(\S\S)\s\*/, "\\1 ")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Fix "low aces"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand.gsub!(/L(\S)/, "A\\1")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Remove duplicate aces (this will not work if you have  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# multiple decks or wild cards)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand.gsub!(/((A\S).\*)\2/, "\\1")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# cleanup white space  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand.gsub!(/\s+/, ' ')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# careful to use gsub as gsub! can return nil here  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand.gsub(/\s+$/, '')  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;# ...

This just restores the ace back to its usual display.

Now we can see both of those methods put to good use:

&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def straight\_flush?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (md = (/.(.)(.)(?: 1.\2){4}/.match(delta\_transform(true))))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;high\_card = Card::face\_value(md[1])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand = fix\_low\_ace\_display(md[0] + ' ' +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;md.pre\_match + ' ' + md.post\_match)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[9, high\_card], arranged\_hand]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;# ...

This is similar in function to royal\_flush?(), but you can see that it uses  
delta\_transform() to make it easy to match a straight. fix\_low\_ace\_display() is  
called on the result, before the method returns.

The rest of the hand methods are very similar. Sort the cards, match a pattern,  
return rank and hand or false. Here they are, without further explanation:

&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def four\_of\_a\_kind?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (md = (by\_face =~ /(.). \1. \1. \1./))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get kicker  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(md.pre\_match + md.post\_match).match(/(\S)/)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[8, Card::face\_value(md[1]), Card::face\_value($1)],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrange\_hand(md)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def full\_house?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (md = (by\_face =~ /(.). \1. \1. (.\*)(.). \3./))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand = arrange\_hand(md[0] + ' ' +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;md.pre\_match + ' ' + md[2] + ' ' + md.post\_match)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[7, Card::face\_value(md[1]), Card::face\_value(md[3])],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif (md = (by\_face =~ /((.). \2.) (.\*)((.). \5. \5.)/))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand = arrange\_hand(md[4] + ' ' + md[1] + ' ' +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;md.pre\_match + ' ' + md[3] + ' ' + md.post\_match)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[7, Card::face\_value(md[5]), Card::face\_value(md[2])],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def flush?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (md = (by\_suit =~ /(.)(.) (.)\2 (.)\2 (.)\2 (.)\2/))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::face\_value(md[1]),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\*(md[3..6].map { |f| Card::face\_value(f) })  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrange\_hand(md)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def straight?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if hand.size \> 5  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transform = delta\_transform  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# note we can have more than one delta 0 that we  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# need to shuffle to the back of the hand  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;until transform.match(/^\S{3}( [1-9x]\S\S)+( 0\S\S)\*$/) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transform.gsub!(/(\s0\S\S)(.\*)/, "\\2\\1")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (md = (/.(.). 1.. 1.. 1.. 1../.match(transform)))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;high\_card = Card::face\_value(md[1])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand = fix\_low\_ace\_display(md[0] + ' ' +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;md.pre\_match + ' ' + md.post\_match)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = [[5, high\_card], arranged\_hand]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def three\_of\_a\_kind?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (md = (by\_face =~ /(.). \1. \1./))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get kicker  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand = arrange\_hand(md)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand.match(/(?:\S\S ){3}(\S)\S (\S)/)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::face\_value(md[1]),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::face\_value($1),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::face\_value($2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def two\_pair?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (md = (by\_face =~ /(.). \1.(.\*) (.). \3./))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get kicker  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand = arrange\_hand(md[0] + ' ' +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;md.pre\_match + ' ' + md[2] + ' ' + md.post\_match)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand.match(/(?:\S\S ){4}(\S)/)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::face\_value(md[1]),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::face\_value(md[3]),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::face\_value($1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def pair?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (md = (by\_face =~ /(.). \1./))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get kicker  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand = arrange\_hand(md)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand.match(/(?:\S\S ){2}(\S)\S\s+(\S)\S\s+(\S)/)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::face\_value(md[1]),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::face\_value($1),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::face\_value($2),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Card::face\_value($3)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arranged\_hand  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def highest\_card?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = by\_face  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[1, \*result.face\_values[0..4]], result.hand.join(' ')]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;# ...

Now what we really need to know is which one of those hands was found. The code  
for that isn't overly complex:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OPS = [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['Royal Flush', :royal\_flush?],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['Straight Flush', :straight\_flush?],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['Four of a kind', :four\_of\_a\_kind?],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['Full house', :full\_house?],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['Flush', :flush?],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['Straight', :straight?],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['Three of a kind', :three\_of\_a\_kind?],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['Two pair', :two\_pair?],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['Pair', :pair?],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['Highest Card', :highest\_card?],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def hand\_rating  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OPS.map { |op|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(method(op[1]).call()) ? op[0] : false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}.find { |v| v }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def score  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OPS.map { |op|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;method(op[1]).call()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}.find([0]) { |score| score }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...

The OPS Array maps hand names to the method that will spot them. With that, you  
call call either hand\_rating() or score() which will walk the whole list of  
tests, then return the first one that was true. hand\_rating() returns the name  
while score() returns the rank and hand Array from the hand method call.

Finally, Hand has a few more very basic helper methods:

&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def take\_card(card)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hand.push(card)  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def arranged\_hand  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;score[1] + " (#{hand\_rating})"  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def just\_cards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@hand.join(" ")  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just\_cards + " (" + hand\_rating + ")"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;# ...

The only thing to notice there is the arranged\_hand() is just a shell over  
score() and hand\_rating() and to\_s() is a shell over just\_cards() and  
hand\_rating().

The rest of Patrick's code goes on to build a complete game of Texas Hold'Em  
that plays itself out round by round and displays results as it goes. This is  
very interesting stuff, but it doesn't solve the quiz, the way I read it.  
Luckily, a solution is easy to finish off from here. Here's my solution to the  
quiz, using Partick's classes:

&nbsp;&nbsp;# ...  
&nbsp;&nbsp;  
&nbsp;&nbsp;### code by JEG2 ###  
&nbsp;&nbsp;if \_\_FILE\_\_ == $0  
&nbsp;&nbsp;&nbsp;&nbsp;best = nil  
&nbsp;&nbsp;&nbsp;&nbsp;results = []  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;ARGF.each\_line do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if line.length \< 20 # they folded  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results \<\< line.chomp  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hand = Hand.new(line) # rank hand  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name = hand.hand\_rating  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;score, arranged = hand.score  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if best.nil? or (score[0] \<=\> best[0]) == 1 # track best  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;best = [score[0], results.size]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results \<\< "#{arranged} #{name}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;# show results  
&nbsp;&nbsp;&nbsp;&nbsp;results.each\_with\_index do |e, index|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts(if index == best[1] then "#{e} (winner)" else e end)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

That should be pretty straight forward by this point. I setup variables to  
track the best hand and the complete results, parse input, handle folds, score  
each hand, remembering to track the best so far, and finally out the results.  
That funny compare, (score[0] \<=\> best[0]) == 1, is because the grade returned  
by score is actually an Array of values and Array implements \<=\> but not \>; go  
figure. That gets me the following output for the quiz example:

&nbsp;&nbsp;Ks Kd Kc 9s 9d 6d 3c Full house (winner)  
&nbsp;&nbsp;Ks Kd 9d 9c Ah 6d 3c Two pair  
&nbsp;&nbsp;Ac Qc Ks Kd 9d 3c  
&nbsp;&nbsp;9h 5s  
&nbsp;&nbsp;Kd 9d 6d 4d 2d Ks 3c Flush  
&nbsp;&nbsp;7s Ts Ks Kd 9d

While I'm showing output, check out this neat variation by Derek Wyatt:

&nbsp;&nbsp;9d 9s Kd Ks Kc 3c 6d Full House (Kings over Nines) (winner)  
&nbsp;&nbsp;9d 9c Kd Ks Ah 3c 6d Two Pair (Kings and Nines)  
&nbsp;&nbsp;Ac Qc Ks Kd 9d 3c  
&nbsp;&nbsp;9h 5s  
&nbsp;&nbsp;Ks Kd 2d 4d 3c 6d 9d Pair (Kings)  
&nbsp;&nbsp;7s Ts Ks Kd 9d

I love the way it gives you extra details about the hand, but as you can see we  
don't agree on hand number four. Don't sweat that though, seems everyone had a  
good round of bug hunting for this one.

My thanks to all the card sharks out there. I also want to thank Patrick for  
writing code I could figure out how to hijack. This summary was definitely a  
team effort.

Tomorrow, Tymothy Byrd will hit you with a brain bender you and Ruby can work  
together to solve...
