Skip to content

Instantly share code, notes, and snippets.

@eric1234
Last active November 27, 2018 21:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric1234/375ad4a79972467d6f30af3bd0146584 to your computer and use it in GitHub Desktop.
Save eric1234/375ad4a79972467d6f30af3bd0146584 to your computer and use it in GitHub Desktop.
Easy Sharing of Data Loading in Rails Controllers
module Lettable
def let name, &blk
iv = "@#{name}"
define_method name do
return instance_variable_get iv if instance_variable_defined? iv
instance_variable_set iv, instance_eval(&blk)
end
helper_method name
define_method :"#{name}=" do |value|
instance_variable_set iv, value
end
private :"#{name}="
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment