Skip to content

Instantly share code, notes, and snippets.

@cnocon
Last active December 26, 2015 16:48
Show Gist options
  • Select an option

  • Save cnocon/7182103 to your computer and use it in GitHub Desktop.

Select an option

Save cnocon/7182103 to your computer and use it in GitHub Desktop.
For blog post on memoization and recursion
# When this method is called on any class instance,
# the method will reconstruct the string every time.
def user_full_name
"#{first_name} #{last_name}"
end
# By memoizing the call, we store the string with the
# object that is the class instance. Below is a classic
# example of memoization in code:
def user_full_name
@user_full_name ||= "#{first_name} #{last_name}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment