Is A Site For Ruby Idioms Needed? [Update: Yes!]

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 =&gt; 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!

No related posts.

3 Responses to “Is A Site For Ruby Idioms Needed? [Update: Yes!]”

  1. Dave Says:

    I’m the guy on Reddit who offered to help you build the site. My email is attached to the reply here, feel free to use it. ;)

    A quick note about what I was thinking:

    I use IRC all the time, and I idle #ruby on freenode, and one thing I noticed is I was writing a lot of simple boilerplate code to demonstrate the ‘ruby’ way of doing certain things. I thought “Hey, if I’m writing all of these anyway, why not make them searchable and put them some place where people can benefit. And whats more, if I’m writing these sorts of things, other people must be as well… maybe we could build a nice site to aggregate them!” I’m not a ‘pro’ at web dev or Ruby, so I thought building something like this would be a nice way to gain some more experience and give back to the community.

    Let me know what you think, and when you’d like to get started and we can talk about languages and what not. I can host this for a while on my personal VPS, so that shouldn’t be an issue.

  2. alan Says:

    Well, I just registered ruby-idioms.com, so I’ll be looking to build out the site soon, probably starting with heroku for ease of use. COme by #fv.rb on freenode to chat.

  3. Ben Says:

    This would be a great resource. I like refactormycode.com, but a “ruby way” library would be a great resource.

Leave a Reply