Skip to content

Instantly share code, notes, and snippets.

@backpackerhh
Last active August 29, 2015 13:56
Show Gist options
  • Save backpackerhh/9033535 to your computer and use it in GitHub Desktop.
Save backpackerhh/9033535 to your computer and use it in GitHub Desktop.
Dynamic methods with dynamic instances variables and memoization
%w(a b c).each do |var|
define_method var do
ivar = "@#{var}"
if instance_variable_defined?(ivar)
instance_variable_get(ivar)
else
instance_variable_set(ivar, Expensive.request)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment