From: Ken Mitchell Date: 2007-04-24T07:01:38+09:00 Subject: Re: Cards Class --=-MLCSznghyTYcf5RiceNm Content-Type: text/plain Content-Transfer-Encoding: 7bit As a newcomer, you are in a perfect position to learn how to properly document your work. Not only "proper" in your eyes, but it is so easy to make it compatible with rdoc. For instance: ## # Has properties resembling that of a deck of cards # Class Deck ## # Constructs and returns an instance of Deck # def initialize ....... end ## # Does some action to an instance of Deck. Argument foo must be of type string..... # def bar(foo=nil) ........ end end Running this through rdoc will quickly create some helpful results, especially as your code grows and you begin to reuse as much as possible. On Tue, 2007-04-24 at 06:16 +0900, 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 > end > > def draw > @top_card = @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 > > 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 > > end > -- Thanks, Ken Mitchell Wireless Operations Engineer Farmers Wireless CONFIDENTIALITY NOTICE: This e-mail and any attachment to it may contain confidential and legally privileged information. This information is intended only for the recipient named above. If you are not the intended recipient, take notice that any disclosure, copying, distribution or taking of any action based upon this information is prohibited by law. If you have received this e-mail in error, please return it to the sender and delete this copy from your system. Thank you. --=-MLCSznghyTYcf5RiceNm--