Skip to content

Instantly share code, notes, and snippets.

@eternal44
Last active August 29, 2015 14:27
Show Gist options
  • Save eternal44/d198c4d0a47988c2f478 to your computer and use it in GitHub Desktop.
Save eternal44/d198c4d0a47988c2f478 to your computer and use it in GitHub Desktop.
Practice
# prints line numbers before each line & aligns the ":"
class ListGenerator
def column_alignment(lines)
lines.map.with_index(1) do |output, line_number|
"Line #{number_spacing(lines, line_number)}#{line_number}:"\ # rewrite method calls
"#{line_spacing(output, lines)}#{output}" # rewrite method calls
end
end
private
def number_spacing(lines, line_number)
max_line_digits = lines.count.to_s.size
" " * (max_line_digits - line_number.to_s.size)
end
def line_spacing(output, lines)
longest_value = lines.group_by(&:size).max.first
" " * (longest_value - output.size + 1)
end
end
if __FILE__ == $PROGRAM_NAME
lines = []
5.times do
lines << ["big", "ho", "big", "elephant", "z"]
end
lines = lines.flatten
puts ListGenerator.new().column_alignment(lines)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment