I’ve noticed lately that there is definitely a “Ruby way” to write Ruby code. When I first read Effective Perl Programming years and years ago I went from writing code that looks like this (note that I know the “FILE” is wrong, but the wordpress auto-syntax highlighter thingy doesn’t seem to deal well with the correct bracketed syntax):
while (my $line = FILE ) {
if( $line =~ /foo/ ) {
print "$line";
}
} |
(which isn’t all that un-perl-y to begin with, but that’s 15 years of perl in my brain stopping me from writing really un-idiomatic code) to far more idiomatic:
while (FILE) {
print if /foo/
} |
The point being that there are certain conventions and ways that your programming style will adapt to the given language. The following loop
for (i = 0; i < 10; i++) { } |
Is perfectly natural in C, but if you saw it in perl or ruby, while it might be perfectly valid, it would look way out of place in either language, and you’d get funny looks if you presented it to a code review.
In ruby some of these would be to not do this:
t.gsub!(/(http|https)biglonguglyandhardlyworksregex/, "<a href="\">\1</a>") |
But instead do this:
URI.extract(t, %w[ http https ftp ]).each do |url|
t.gsub!(url, "<a href="\">#{url}</a>")
end |
Or maybe instead of this
sum = 0
list = [1,2,3]
for i in list do
sum += i
end
# sum is now 6 |
you use the must more awesomeer (yes it’s a word)
list = [1,2,3]
sum = list.inject(0) { |s,v| s + v } # sum => 6 |
Of course, you can write bad code in almost any language pretty easily (note how I’m not making a php/python/whitespace/brainfuck joke here!)
So my question is this. Is there some grand collection of these Ruby idioms? Is there a need for them? Would a fusion of StackOverflow and Refactor My Code be a useful collection to have somewhere? Or are the resources out there (which are a bit scattered) good enough?
So far I’ve found:
Alternatively there are a lot of places where you can read other’s code to learn idiomatic Ruby by osmosis. Github, RefactorMyCode, the popular Gems in the community, The Ruby Quiz are all great resources for this.
My vision is a melding of StackOverflow (maybe using their new StackExchange community software?) and Refactor My Code where you can search for an idiom or programming operation based on code, Class, or tag, vote, and comment or submit a different version. Sort of like a Perl Golf contest except instead of the fewest keystrokes being the goal it’s the cleanest/nicest/most effective way of doing the operation.
So what do you think, would this be useful to work on with the Ruby community, or is there enough information out there already that is google-able enough? Everyone will also have their own way of doing things, but Ruby is an opinionated language (or is that only Rails?) so maybe there is One (or two) “correct” ways to do things.
Your thoughts appreciated.
Update – So a bit of discussion here, and lots of great comments on proggit show me that this idea does deserve a go of it. I’ve registered ruby-idioms.com (pointed here for now) and hope to have something up in the next couple of weeks, and will take a few beta tests to have a run at it. Keep an eye here for any news by subscribing to the RSS or following me on Twitter. Thanks everyone!