Skip to content

Instantly share code, notes, and snippets.

@dbrady
Created March 12, 2012 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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.
@jerith
Copy link

jerith commented Mar 12, 2012

Can you not draw both sets of columns at once? Instead of two iterations, why not one that does something like this:

(0..PAGE_WIDTH).step(COLUMN_WIDTH).each do |x|
  draw_vertical_line_at x
  draw_vertical_line_at x + CHECK_COLUMN_WIDTH if x < PAGE_WIDTH
end

That pretty clearly expresses "I'm drawing columns, each of which has a check column in it". (To me, at least.)

Update, because I forgot to actually write this bit down. I think the special-case to avoid drawing the fencepost check column is a bit ugly, and I'm not convinced it's valid if x is a float. Fixing that elegantly is left as an exercise for the reader.

@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