h1. Dan's Ruby Style Guide h2. Introduction Consistent source is great. Especially on multi-developer projects, it really helps to be able to open a file you may or may not have written and know it's going to look similar with everything else in the project. If it doesn't, it can distract you and take concentration away from what you should really be working on. Spend some extra time before committing (or, better yet, while writing) code that goes along with the project you're working on and it will be much easier to come back and change in the future. h2. General Rules There are a few things that affect the source as a whole. * Indent code two spaces at a time * Use underscores for separation in variable/method names * Use && and || instead of and and or * Lines should kept under 80 columns wide * Use single blank lines between methods, classes, etc. * Care should be taken to avoid erroneous whitespace h2. Strings * Use ' (single quotes) for strings that don't need interpolation and " (double quotes) for strings that do. Example:
  simple_string = 'one'
  complex_string = "complex string #{simple_string}"
h2. Arrays * Use [] to define an empty/new Array * Use [element_1, element_2, ...] to define a new Array with elements h2. Hashes * Use {} to define an empty/new Hash * Use { :foo => :bar } (note spacing) to define a new Hash with keys and values * Avoid extra braces. Example: my_hash.update(:x => 'y') instead of my_hash.update({ :x => 'y' }) h2. Blocks Since there are two ways to define blocks ({ .. } and do ... end) it can be hard to decide which variation to use and when. I use { ... } for simple and short blocks that fit under 80 columns, such as:
  the_thing = things.detect {|t| t.foo? }
For more complex blocks, even ones that fit in 80 columns, I use do ... end, such as:
  the_thing = things.detect do |thing|
    thing.blue? && thing.bright?
  end
h2. Classes * Indent methods below protected, private, or public. Example:
  class MyClass
    def my_public_method
      ...
    end
    
    protected
      def my_protected_method
        ...
      end
  end