Skip to content

Instantly share code, notes, and snippets.

@eric1234

eric1234/lettable.rb

Last active Nov 27, 2018
Embed
What would you like to do?
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