Skip to content

Instantly share code, notes, and snippets.

@dbrady
Created March 12, 2012 17:34
Show Gist options
  • Save dbrady/2023535 to your computer and use it in GitHub Desktop.
Save dbrady/2023535 to your computer and use it in GitHub Desktop.
Iterating by columns with a nudge factor
We couldn’t find any files to show.
@baroquebobcat
Copy link

Why not

COLUMNS.times do |i|
  draw_vertical_line_at COLUMN_WIDTH*i
end
draw_vertical_line_at PAGE_WIDTH

COLUMNS.times do |i|
  draw_vertical_line_at (COLUMN_WIDTH)*i+CHECK_COLUMN_OFFSET
end

?

Then it's clear you are drawing COLUMNS columns, with a following border. In addition, there's another line after. Explicitly making the trailing line implies to the reader that the important nouns are the columns not the lines.

Because of the focus on columns, it's also more composable.

def draw_basic_columns
  COLUMNS.times do |i|
    draw_vertical_line_at COLUMN_WIDTH*i
    yield i
  end
  draw_vertical_line_at PAGE_WIDTH
end

def draw_check_columns
  draw_basic_columns do |i|
    draw_vertical_line_at (COLUMN_WIDTH)*i+CHECK_COLUMN_OFFSET
  end
end

def draw_smiley_columns
  draw_basic_columns do |i|
    draw_smiley_at (COLUMN_WIDTH)*i+SMILEY_COLUMN_OFFSET
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment