Slide 1

Slide 1 text

Beautiful Ruby code formatting Florent Guilleux, RubyPerú October 2012 PDF: http://goo.gl/MxECI

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Los gustos no se discuten pero sí se cultivan. Adrienne Maillet

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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)

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

@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

Slide 15

Slide 15 text

regexp = /start\s(group)(?:alt1|alt2)end/

Slide 16

Slide 16 text

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/

Slide 17

Slide 17 text

A final exercise • take an existing piece of your code and try to make it beautiful • publish the gist!

Slide 18

Slide 18 text

The happy programmer