Beautiful Ruby code formatting

Beautiful Ruby code formatting

A few examples of pieces of Ruby beautifully formatted

Ad6ce40f9aa3fb8817b7299baf0f8484?s=128

Florent Guilleux

October 04, 2012
Tweet

Transcript

  1. Beautiful Ruby code formatting Florent Guilleux, RubyPerú October 2012 PDF:

    http://goo.gl/MxECI
  2. None
  3. None
  4. Code formatting is important. It is too important to ignore

    and it is too important to treat religiously. Code formatting is about communication, and communication is the professional developer’s first order of business. Clean Code, Robert Martin
  5. Los gustos no se discuten pero sí se cultivan. Adrienne

    Maillet
  6. layout_was = @default_layout @default_layout = false template = compile_template(engine, data,

    options, views) output = template.render(scope, locals, &block)
  7. layout_was = @default_layout @default_layout = false template = compile_template(engine, data,

    options, views) output = template.render(scope, locals, &block) layout_was = @default_layout @default_layout = false template = compile_template(engine, data, options, views) output = template.render(scope, locals, &block) from Sinatra
  8. unless valid_month?(@start_month) && valid_start_year?(@start_year) || valid_issue_number?(@issue_number) errors.add :start_month, "is invalid"

    unless valid_month?(@start_month) errors.add :start_year, "is invalid" unless valid_start_year?(@start_year) errors.add :issue_number, "cannot be empty" unless valid_issue_number?(@issue_number)
  9. unless valid_month?(@start_month) && valid_start_year?(@start_year) || valid_issue_number?(@issue_number) errors.add :start_month, "is invalid"

    unless valid_month?(@start_month) errors.add :start_year, "is invalid" unless valid_start_year?(@start_year) errors.add :issue_number, "cannot be empty" unless valid_issue_number?(@issue_number) unless valid_month?(@start_month) && valid_start_year?(@start_year) || valid_issue_number?(@issue_number) errors.add :start_month, "is invalid" unless valid_month?(@start_month) errors.add :start_year, "is invalid" unless valid_start_year?(@start_year) errors.add :issue_number, "cannot be empty" unless valid_issue_number?(@issue_number) from active_merchant
  10. def map_to_table(table_name) features << Relation.new(:name => table_name, :db => BrokenRecord.database,

    :record_class => self) end
  11. def map_to_table(table_name) features << Relation.new(:name => table_name, :db => BrokenRecord.database,

    :record_class => self) end def map_to_table(table_name) features << Relation.new(:name => table_name, :db => BrokenRecord.database, :record_class => self) end from broken_record
  12. def to_liquid self.data.deep_merge({ "url" => self.url, "date" => self.date, "id"

    => self.id, "categories" => self.categories, "next" => self.next, "previous" => self.previous, "tags" => self.tags, "content" => self.content }) end from Jekyll
  13. @regions.select { |r| distance >= r[:minimum_distance] }.max_by { |r| r[:minimum_distance]

    }.fetch(:label)
  14. @regions.select { |r| distance >= r[:minimum_distance] }.max_by { |r| r[:minimum_distance]

    }.fetch(:label) @regions.select { |r| distance >= r[:minimum_distance] } .max_by { |r| r[:minimum_distance] } .fetch(:label) from blind
  15. regexp = /start\s(group)(?:alt1|alt2)end/

  16. regexp = %r{ start # some text \s # white

    space char (group) # first group (?:alt1|alt2) # some alternation end }x from Ruby Style Guide regexp = /start\s(group)(?:alt1|alt2)end/
  17. A final exercise • take an existing piece of your

    code and try to make it beautiful • publish the gist!
  18. The happy programmer