Fixnum playing cards

I've been playing with ruby's playinc cards,and to make life easy my
cards are 1..54, and I've been adding things to Fixnum directly [1].
I've not used a language which lets me redefine numbers before, so not
sure how much trouble I can get my self into :slight_smile:

Is this sane?

Douglas

[1] Code listing:

module PlayingCards
  
  def black?
    self > 26
  end
  
  def suit
    case self
      when 01..13; :hearts
      when 14..26; :dimonds
      when 27..39; :clubs
      when 40..52; :spades
      when 53..54; :joker
    end
  end
  
  def face
    return case n = (self - 1) % 13 + 1
      when 2..10; n.to_s.to_sym
      when 01; :Ace
      when 11; :Jack
      when 12; :Queen
      when 13; :King
    end unless self > 52
    
    return case self
      when 53; :Red
      when 54; :Black
    end unless self > 54
  end
  
  def card_name
    return "#{face} of #{suit}" unless self > 52
    return "#{face} #{suit}" unless self > 54
  end
  
end

class Fixnum
  include PlayingCards
end

cards = (1..54).to_a.sort_by{rand}

puts cards.collect{|card| card.card_name}

puts "\nYou've got card! It is the #{cards.pop.card_name}."

Douglas Livingstone ha scritto:

just a thing: you don't need to convert 1..54 to an array before you use sort_by, sort_by is available in Ranges too.

It looks like 53 (Red Joker) would be classified as black (53.black?
== true) since 53 > 26. Maybe change to:

  def black?
     self > 26 and self != 53
  end

Also, if we're defining black? why not define red? as well:

  def red?
    not black?
  end

Jacob Fugal

···

On 4/13/05, Douglas Livingstone <rampant@gmail.com> wrote:

[1] Code listing:

module PlayingCards

        def black?
                self > 26
        end

        # ...snip...
        def face
                # ...snip...
                return case self
                        when 53; :Red
                        when 54; :Black
                end unless self > 54
        end

        # ...snip...
end

Or even neater, change the range from 0..53, and make it [joker, cards,
joker]

(are there red and black jokers? thought the two were identical)

martin

···

Jacob Fugal <lukfugl@gmail.com> wrote:

It looks like 53 (Red Joker) would be classified as black (53.black?
== true) since 53 > 26. Maybe change to:

  def black?
     self > 26 and self != 53
  end

There are big and little jokers, at least, that are differentiated for
the purposes of certain games which are escaping me at the moment.
-Eliah.

···

On 4/13/05, Martin DeMello <martindemello@yahoo.com> wrote:

(are there red and black jokers? thought the two were identical)

I've had some packs of cards that had red and black jokers. I've never
differentiated between them in any games except bartok, but they're still
red and black.

Adelle.

···

On 4/13/05, Martin DeMello <martindemello@yahoo.com> wrote:
> (are there red and black jokers? thought the two were identical)