Is there a style guide for RoR? For example, I see that rails likes
to create things like
<% … %>
<% … %>
<% … %>
That is probably an artifact of a generator. In general, one Rails style
guideline is: Don’t Lean on Generators. The point of Rails is to write
minimal code into the terminals of event handlers.
and I’m tempted to write this as
Write it like that. However, I’m tempted to write it like this:
x = Builder::XmlMarkup.new
Sometimes I write whole stretches of HTML not in ERb, but in Ruby as an
XmlMarkup. Sometimes that makes it easier to refactor.
Since I’m a beginner, I’d like to get into the habit of writing code
that has a conventional look. Years of C coding have convinced me
that divergent writing styles [e.g. KR versus GNU] can slow the
transfer of ideas. And I know that Ruby has this idea of “beauty”.
What I’m wondering is whether someone has written some examples of
beautiful, and ugly, RoR code.
If you have a team, follow what the team does. That fixes the “K&R vs
nonsense. All code should look like only one (very smart) person wrote
Its technical and esthetic style should be seamless.
Your code should…
- follow team guidelines
- pass all tests (and have tests for every detail)
- be clear and expressive
- obey Don’t Repeat Yourself
Under Don’t Repeat Yourself, that <% %><% %><% %> sample you posted is
very DRY, because it repeats those silly delimiters!
Ruby, and especially the Rails Inflector, add a new twist to the “clear
expressive” part. We use underscore_identifiers, not camelCase, despite
former is slightly harder to type. We do it because the _ looks as close
possible to a space.
Well formed Ruby statements should read like normal English.
Consider ‘Order.has_many :line_items’. That Ruby is so clearly English
it expresses a program requirement. Even a client could read it (if not
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!