Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beautiful Ruby code formatting

Beautiful Ruby code formatting

A few examples of pieces of Ruby beautifully formatted

Florent Guilleux

October 04, 2012
Tweet

More Decks by Florent Guilleux

Other Decks in Programming

Transcript

  1. 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
  2. layout_was = @default_layout @default_layout = false template = compile_template(engine, data,

    options, views) output = template.render(scope, locals, &block)
  3. 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
  4. 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)
  5. 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
  6. 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
  7. 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
  8. @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
  9. 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/
  10. A final exercise • take an existing piece of your

    code and try to make it beautiful • publish the gist!