Created
November 10, 2016 13:33
-
-
Save armw4/f42e43f31d88cb15cf7f5505c282f99a to your computer and use it in GitHub Desktop.
Prints out a multiplication table. Written in Ruby for brevity. .NET equivalents in comments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## To execute, install Ruby, download file, and run: | |
# ruby mult-table.rb | |
THREE_SPACES = ' ' * 3 # new String(' ', 3) | |
def print_header(rows) | |
print THREE_SPACES # Console.Write(...) | |
1.upto(rows) { |number| print "#{THREE_SPACES} #{number}" } # String.Format(...) | |
puts # Console.WriteLine(...) | |
end | |
def print_rows(rows) | |
1.upto(rows) do |row| # Enumerable.Range(...) | |
print "#{THREE_SPACES} #{row}" | |
1.upto(rows) { |number| print "#{THREE_SPACES} #{row * number}" } | |
puts | |
end | |
end | |
def print_table(rows) | |
print_header(rows) | |
print_rows(rows) | |
end | |
print_table(3) ## => 1 2 3 | |
# 1 1 2 3 | |
# 2 2 4 6 | |
# 3 3 6 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment