views:

91

answers:

4

I've just started writing Ruby code from a C++ background. When I write the code, I find myself writing C++ code using the Ruby keywords(using while and for loops when a single Ruby command will make the code much shorter). Are there any sites which you use where people can look at code and recommend more optimal ways to use the language? For me its Ruby specific, but I'm sure this would apply for anyone transitioning to a new language.

A: 

Just ask it on here. SO helps me whenever I need to "Rubyfy" some code. I'm at a point where I never use for/while loops in Ruby anymore.

CookieOfFortune
+4  A: 

Here are a few places to look for starters:

The code kata idea is an interesting one: take a fairly straightforward problem and solve it. Dig around, learn some more idiomatic stuff and try it again. Repeat. Have fun.

Mike Woodhouse
A: 

Try going through the whole tutorial to get a feel for the language.

Normally it takes some time working with a language to know it well. I'd suggest to just write code.

Georg
A: 

There's even automated code-o-metrics tool, called Ruby Sadist. Give it a try - it finds the least Rubyish pieces of code with 'flog' and blocks of repeating code with 'flay'. Works OK for me - very handy in refactoring.

But them won't help you write n.times instead of for loops:( That's what Ruby Best Practices (both book and blog) are for.

(Little offtopic, maybe) I think that the best Ruby code should be the fastest one. It's strange, but the shortest code often performs best. Use Benchmark module to determine the best way, if you wish.

kirushik