Skip to content

Instantly share code, notes, and snippets.

@philcallister
Last active August 29, 2015 14:03
Show Gist options
  • Save philcallister/c3fc0ead7074999152ff to your computer and use it in GitHub Desktop.
Save philcallister/c3fc0ead7074999152ff to your computer and use it in GitHub Desktop.
Multiplication Table
def mult_table(num, spaces)
max_space = (num * num).to_s.length + spaces
rows = []
(1..num).each do |row|
rows << (1..num).inject('') { |r,col| r += sprintf("%#{max_space}d", row * col) }
end
rows
end
mult_table(12, 2).each { |row| puts row }
mult_table(4, 5).each { |row| puts row }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment