From: dblack@... Date: 2007-06-12T19:59:10+09:00 Subject: Re: a matter of style Hi -- On Tue, 12 Jun 2007, Anthony Martinez wrote: > On Tue, Jun 12, 2007 at 03:10:49AM +0900, 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: > [snip] >> 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/ >> >> and give me some suggestions on style / ruby idiom / other things? >> >> Any help would be greatly appreciated, > > From what I've been led to understand, method names consisting of > multiple words should be named_with_underscores, not namedWithCamelcase. > > `puts' is the same as `print "something\n"' Almost. puts adds "\n" unless it's already there. So these are equivalent: print "hello\n" puts "hello\n" > Parens can be omitted if it doesn't confuse the parser (or the reader) Can be, but it's best not to get too cutesy with it :-) In particular, please have mercy and don't leave the parens out in method signatures. Things like this: def a b, c, d = 1 read very strangely, at least to my eyes. > As well, reserve the { } form of blocks to one-liners. That's going to depend partly on whether you run across the (relatively rare) case where the precedence difference between {} and do/end actually matters. There are also some interesting ideas on record (see archives) involving blocks with side effects vs. blocks that just calculate. But I can't remember which is supposed to be which :-) > Then again, these are highly subjective views, ones that I've absorbed > from reading other people's ruby and getting into arguments with friends > :) Take them with a grain of salt, as I'm not a Ruby expert and thus I > might be wrong. Reading Ruby code, in particular the Ruby source code itself, is a great way to see the traditional style. I've never seen anyone improve on that style. People get very excited about the fact that Ruby lets you "do your own thing" in terms of style -- like: printf( "My name is %s\n", thisPerson.name() ); and such... but if most Ruby were written like that I think I would have put the Pickaxe back on the shelf at the bookstore :-) David -- * Books: RAILS ROUTING (new! http://safari.awprofessional.com/9780321509246) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)