From: Joseph Seaton Date: 2007-04-24T07:01:14+09:00 Subject: Re: Cards Class Nice work! I'm an utter newbie (probably more of one than you) but since you asked for comments... 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!): > > class Deck > @@Cards = > ['Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King','Ace'] > @@Suits = ['Spades','Hearts','Diamonds','Clubs'] > > def initialize > @top_card = -1 > @deck = [] > 52.times do |card| > @deck << card > end > @deck = (0..52).to_a > end > > def draw > @top_card = @top_card + 1 > @top_card += 1 > if @top_card < 52 > @deck[@top_card] > else > raise 'There are only 52 cards in a deck!' > end > end > > def shuffle > @top_card = -1 > @deck = @deck.sort_by {rand} > end > Nice trick there by the way, I'll have to remember that one > def suit > @@Suits[@top_card/13] > end > > def face > factor = @top_card/13 > index = @top_card - 13*factor > @@Cards[index] > end > > def set_card(card) > @top_card=card > end > > attr_writer :top_card would be simpler > end > > I look forward to reading your code when you nolonger consider yourself a ruby newbie Joe