From: "Jesús Gabriel y Galán" Date: 2007-04-24T17:46:36+09:00 Subject: Re: Cards Class On 4/23/07, Peter Marsh wrote: > Hi, I'm an utter newbie to Ruby (and OOP!) and I thought I'd share my > very first project. It's nothing special - just a simple class that > simulates a deck of cards, but I'd like to get your comments to see if > there's anything I can do better (enjoy!): Others have commented your code, so I'll just share a Deck class I made for some little things. It may give you some ideas or maybe someone else will comment on it. It can contain any object, not just normal cards. Also I want to implement some shuffle methods that resembled a human shuffle (with it's fuzziness) apart from the actual random sorting, but never got to do it, I just implemented a fuzzy cut in half. Here it is: require 'emptydeckerror' class Deck def initialize @deck = [] end def << (item) @deck << item self end def draw item = @deck.shift raise EmptyDeckError, "The deck is empty" if !item item end def cut!(fuzzy = false) cutpoint = @deck.size / 2 - 1 cutpoint = fuzzy(cutpoint) if fuzzy half = @deck.slice!(0..cutpoint) (@deck << half).flatten! self end def randomize! @deck = @deck.sort_by {rand} self end def to_s @deck.to_s end def empty? @deck.size == 0 end protected def fuzzy(number) # Deviation: 10% of the deck size (min 1) deviation = @deck.size / 20 deviation = 1 if deviation == 0 randmax = deviation * 2 + 1 number + rand(randmax) - deviation end end Jesus.