Skip to content

Instantly share code, notes, and snippets.

@TravisL12
Last active December 20, 2015 18:19
Show Gist options
  • Select an option

  • Save TravisL12/6175381 to your computer and use it in GitHub Desktop.

Select an option

Save TravisL12/6175381 to your computer and use it in GitHub Desktop.
# Andrews' CODE
def times_table(rows) # <-- defines method and how many rows passed as argument
1.upto(rows) do |x| # <-- You start first loop with value x
line = ""
1.upto(rows) do |y| # <-- begin 2nd (nested) loop with value y
line += "#{x*y}\t" # <-- x=1 therefore 1*1, 1*2, 1*3. When done x=2 2*1, 2*2, 2*3.... (x=3, 3*1, 3*2, 3*3...)
end
puts line
end # <-- incremement to the next x value
end
# The basics are, that you will increment an 'x' value and then run the entire 'y' loop.
# Then you increase 'x' and then run the whole 'y' loop again.
# An alternative looping system could be written without the 'line' variable:
1.upto(rows) do |x|
1.upto(rows) do |y|
print "#{ x * y }\t"
end
puts
end
# Take note of why the 'puts' is placed after the 'y' loop.
# That's to give a line break after each row of values has been printed.
@AndrewGuard
Copy link

Interesting. Your way is definitely better. Thanks for clearing that up. My line spacing was redundant with the \t.

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