Skip to content

Instantly share code, notes, and snippets.

@clay-whitley
Last active December 20, 2015 00:59
Show Gist options
  • Select an option

  • Save clay-whitley/6046160 to your computer and use it in GitHub Desktop.

Select an option

Save clay-whitley/6046160 to your computer and use it in GitHub Desktop.
Explaining the differences between typical methods and recursive methods for my blog.
# Typical method definition
def multiply_number(integer)
puts integer * 2
end
# Typical method calls
multiply_number(5) # this would print to the console the result of 5 * 2
multiply_number(23) # this would print to the console the result of 23 * 2
# Recursive method definition
def count_down_from(integer)
if integer < 1
puts "This method is all done!"
return
end
puts integer
count_down_from(integer - 1) # here is the recursive call,
# the method calls itself INSIDE it's own definition.
end
# Recursive method call
count_down_from(5)
# The above method call would print to the console the numbers 5,4,3,2, and 1,
# followed by the string "This method is done!"
count_down_from(100) # This call would print to the console numbers from 100 to 1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment