From: Robert Dober Date: 2007-06-12T03:58:21+09:00 Subject: Re: a matter of style On 6/11/07, Bas van Gils wrote: > > All, > > A little while ago I picked up a copy of the pickaxe book and got hooked to > Ruby. Never had a `real' project to work on though (that's what you get when > you don't hack for a living I suppose :-). Either way, a few weeks back I > needed an app that generates deals for the card game bridge. I made a small > design and hacked up some unit tests. After a while no more tests failed: > > bas@Librarian { ~/bridgehands }$ ruby bridgeTests.rb > Loaded suite bridgeTests > Started > .... > Finished in 0.003154 seconds. > > After a bit more coding to format the output I got something that outputs: > > bas@Librarian { ~/bridgehands }$ ruby deal.rb > AQJ652 > 92 > T53 > Q5 > 973 4 > K87653 AQJ > 8 J742 > JT4 A9876 > KT8 > T4 > AKQ96 > K32 Do not forget to unblock the Ten of Diamonds in your 4 Spades ;) > > Not bad... Like I said, I'm not a full-time programmer. I would *love* to > learn more about `proper' ruby style though. Is there even such a thing? I > mean, the book cover said something about *pragmatic* :-) > > Could anyone have a look at: > > http://www.van-gils.org/~bas/bridgehands/ Some thaughts (*) why do you define Player#setName! an attribute accessor to :name seems appropriate (*) Card = Struct.new( :suit, :face ) would be sufficent (*) Ruby allows for much shorter code, look e.g at def getCard if @cards.size == 0 raise "No more cards in the deck" end return @cards.slice!(0) end which I would write as def getCard; @cards.shift or raise SomeNiceErrorClass, "No more cards in the deck" end (+) An example where you are in harmony with ruby is: def cardsOfSuit(sn) return @cards.select{ |card| card.suit.name==sn } end The return however is superfluous, but it is a respectable style to end all defs with return statements (*) Personally I think it is nice to name CardSet#shuffle! with the "!". However more learned members of the community think that the existance of X#member! implies X#member and X#member is to be defined as def member; x = dup; x.member!; x end Just to let you know, I am with you but we are pretty alone ;) All at all pretty impressive for a beginner. Cheers Robert > > and give me some suggestions on style / ruby idiom / other things? > > Any help would be greatly appreciated, > > yours > > Bas > > -- > Bas van Gils , http://www.van-gils.org > [[[ Thank you for not distributing my E-mail address ]]] > Robert -- You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw