Ruby-Poker 0.2.1
Ruby-Poker 0.2.1 is an incremental update over the 0.2.0 release. The biggest change is the addition of the << and delete methods to the PokerHand object. Making it possible to add and remove cards from a hand without creating a new PokerHand object.
require 'rubygems' require 'ruby-poker' hand.PokerHand.new("3d 3s 7h 7d") hand.to_s # => "3d 3s 7h 7d (Two pair)" hand << "7c" hand.to_s # => "3d 3s 7h 7d 7c (Full house)" hand.delete("3d") hand.delete("3s") hand.to_s # => 7h 7d 7c (Three of a kind)"
I’m always in the process of adding documentation to ruby-poker. At this point the majority of the public facing methods of the PokerHand and Card classes have been documented with examples. The ruby-docs are available online at http://rubypoker.rubyforge.org/.
Rails has a low learning curve? Hardly. You need to know Ruby

Ruby on Rails is often (incorrectly) billed as the framework that makes web development easy. Unfortunately a lot of people take this to mean “Anyone can make a web site with Rails” or “You can get started with Rails in 15 minutes“. Unfortunately neither is the case but regardless hundreds of thousands of people1 are flocking to Rails to start making a web app for everything under the sun.
These people learn in hurry that Rails is actually quite a large beast2 and jump right into working with Rails without taking time to learn the programming language that Rails uses… Ruby. Almost like Ruby doesn’t exist. If I had to guess I would say people coding Rails apps without actually knowing Ruby has inevitably lead to the Rails community’s pseudo status as a ghetto.
I must confess, I was one of the Rails coders who didn’t know Ruby when I first started with Rails. Very few people who come to Rails known Ruby due to Ruby’s limited popularity before Rails came along. For about the past 7 months I have been trudging along, learning more about Ruby every day3 and the code I write for Rails projects now is much better. Ruby programming constructs like blocks, lamba, proc, etc are fairly advanced topics that I am probably only beginning to comprehend.
Most people might think it is obvious that you would need to know a framework’s programming language before you started using the framework. Certainly you would not attempt J2EE development without first knowing Java. For some reason this has not always been the case with Rails.
-
Instant Rails has been downloaded over 400,000 times. ↩
-
The Rails Way which contains everything a Rails developer needs to know about Rails is 912 pages. ↩
-
Largely thanks to Project Euler. ↩
What’s New in Ruby-Poker 0.2.0
Yesterday ruby-poker 0.2.0 was released. Here is a synopsis of what has changed.
Hands are no longer limited to 5 cards!
In order to play any poker game other than 5-card draw (like Texas Holdem) you need to evaluate hands containing more or less than 5 cards. Now you can with ruby-poker.
holdem_hand = PokerHand.new("Qc Qd Qs 5d 5h 8c 2h") # Any number of cards you want
HOWTO: Use Ruby-Poker
First install ruby-poker:
gem install ruby-poker
Create a PokerHand object using the cards in your hand. PokerHand takes either an array of the cards or a string with a space between each card.
hand1 = PokerHand.new("8H 9C TC JD QH") hand2 = PokerHand.new(["3D", "3C", "3S", "13D", "14H"])
hand1 was instantiated using a string and hand2 was created with an array. The format for entering cards is ValueSuit where value is the numerical value of the card and Suit is the first letter of the card’s suit: Hearts = “H”, Spades = “S”, Clubs = “C”, Diamonds = “D”. For face cards you can use the first letter of its name or the card’s equivalent numerical value.
Ten = T or 10
Jack = J or 11
Queen = Q or 12
King = K or 13
Ace = A or 14
As of ruby-poker 0.2.0 face cards can only be created using letters.
Now that we have created our PokerHands we can find out what their rank is.
puts hand1.rank #=> Straight puts hand2.rank #=> Three of a kind
Or compare the hands to see which one has a higher rank.
puts hand1 > hand2 #=> true
That covers the basic operations. As a bonus tip, if you want to compare more than two hands you can place them in an array and call Array#sort. After sorting, the last hand in the array is the winning hand.
hand3 = PokerHand.new("2H 4C 6D 8S TS") puts hand3.rank #=> High Card puts [hand1, hand2, hand3].sort.last #=> 8h 9c Tc Jd Qh (Straight)
Edited 01/21/08: Updated for ruby-poker 0.2.0
Ruby-Poker: A Poker library for Ruby
In my never ending quest for 100% completion of Project Euler I came to Problem 54 which required determining the winner in 1000 hands of 5 card poker. At first I thought this would be no big deal. Determining whether one hand is greater than another in poker? There’s got to be a library for doing that right? Unfortunately, it looks like someone was about to do one on RubyForge called libpoker but the project was abandoned right after it was started.
So… there’s no Ruby library. Guess I’m doing it myself. The solution was longer than I excepted, just over 200 lines, which is my longest ProjectEuler solution thus far. After finishing I extracted the poker logic into its own RubyGem to make it easy for other people who wanted to make a simple poker game or whatever else they might need it for.
Creating the RubyForge project and the RubyGem for ruby-poker was more time consuming than I had anticipated having never done it before but overall a pretty smooth process and worked out in the end.
Ruby-Poker installation is a snap. If you have RubyGems installed it’s as simple as:
gem install ruby-poker
Update 01/21/08: I wrote a guide for using ruby-poker.
