Created
April 1, 2014 03:17
Revisions
-
mattconnolly revised this gist
Apr 1, 2014 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,10 +2,12 @@ module Let def let(name, &block) ivar = "@#{name}".to_sym if instance_variable_defined?(ivar) instance_variable_get(ivar) else instance_variable_set(ivar, instance_eval(&block)) end end end -
mattconnolly renamed this gist
Apr 1, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
mattconnolly created this gist
Apr 1, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ module Let def let(name, &block) ivar = "@#{name}".to_sym unless instance_variable_defined?(ivar) instance_variable_set(ivar, instance_eval(&block)) end instance_variable_get(ivar) end end class Thing include Let def result let(:result) { some_expensive_calculation_that_could_return_nil } end end t = Thing.new t.result #=> calculated result that could be nil t.result #=> cached result that could be nil