litany against fear
RailsConf 2009 Rails is from Mars Ruby is From Venus
This is part of my series of notes on RailsConf 2009. Check them all out here.
Learning Ruby will make you a better programmer! Why you should you learn Ruby? It’ll help out with using Rails as well. There’s tons of great references for learning about Ruby: the poignant guide from _why, the Pickaxe, and tons of real books too.
It’s your responsibility to listen to your code and figure out what it needs. You need to write Ruby code that is expressive and simple, but also works within the Rails conventions.
Some of the things that you do that makes Ruby crazy:
- Iteration. Don’t do it on your own. Ruby has powerful ones, use them!
for..incallseachinternally. - Not taking advantage of the sugar that Ruby has. Don’t use ternary operators, use
||=. There’s also a|=operator that can help cut code down drastically. - Don’t use checks against nil or false. Use Ruby’s sense of truthiness to your advantage!
- Making code shorter when it doesn’t help understanding it. Brevity is not the goal, readability is the goal.
- Not using
rescueon methods. - Don’t don’t don’t use
!!. Ruby does not not like clarity. Baseis a lame class name. Naming is really important.- Keep Ruby idiomatic. Named scope are a great example of doing this.
Going over some bad code that generates a URL…it revolves around checking the class of an object and doing some actions with that object. Instead of using a case statement for that, it’s better to push that into the respective classes. This is definitely better since it’s a decent separation of concerns and just cleaner Ruby.
Jumping into Rails performance. Don’t prematurely optimize without knowing what will be slow, but at the same time, don’t write code that will never be fast. There’s some ways that Rails helps out with this, like counter_cache.
Ruby is fun! (and easy!) The more Ruby you know, the better you’ll get at Rails.