# Re: Euchre Hands (#55)

**URL:** <https://www.ruby-forum.com/t/re-euchre-hands-55/95441>\
**Category:** Ruby\
**Created:** [November 21, 2005, 11:27pm UTC](https://www.ruby-forum.com/t/re-euchre-hands-55/95441 "2005-11-21T23:27:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [November 21, 2005, 11:27pm UTC](https://www.ruby-forum.com/t/re-euchre-hands-55/95441/1 "2005-11-21T23:27:44Z")

</div>

Because I’m an idiot and didn’t read the quiz spec properly, or test my  
code properly - I left aces low, and expected all the number cards in  
there, and made a couple of other typos. I also made it faster by  
calculating things only once in the initialize instead of constantly in  
the sort\_value method.

Fixed now:

class Card  
attr\_reader :suite, :value  
def initialize(string)  
@value = string[0…-2].upcase  
@suite = string[-1…-1].downcase  
end  
def to\_i  
%w{9 T J Q K A}.index(@value)  
end  
def to\_s  
@value + @suite  
end  
end  
class EuchreHand  
def initialize(trump, cards)  
@trump\_string = trump  
@trump = trump[0…0].downcase  
@cards = cards  
same, opposite = [[‘h’,‘d’],[‘c’,‘s’]].partition {|x|  
x.include?(@trump)}  
same[0].delete(@trump)  
@same = same[0][0]  
@suite\_order = [@trump, opposite[0][0], same, opposite[0][1]]  
end  
def to\_s  
([@trump\_string] +sorted\_cards).join("\n")  
end  
def sorted\_cards  
@cards.sort\_by {|c| sort\_value©}  
end  
private  
#there are 6 sections of the sort: jack1, jack2, trump, opp1, same,  
opp2  
def sort\_value(card)  
return [0] if card.value == ‘J’ && card.suite == @trump  
return [1] if card.value == ‘J’ && card.suite == @same  
return [2+@suite\_order.index(card.suite),-card.to\_i]  
end  
end

cards = ARGF.readlines  
puts EuchreHand.new(cards.shift, cards.map {|s| Card.new(s)})

#####################################################################################  
This email has been scanned by MailMarshal, an email content filter.  
#####################################################################################
