lib/lazy_object.rb in lazy_object-0.1.0 vs lib/lazy_object.rb in lazy_object-0.2.0
- old
+ new
@@ -8,11 +8,11 @@
#
# lazy = LazyObject.new { VeryExpensiveObject.new } # At this point the VeryExpensiveObject hasn't been initialized yet.
# lazy.get_expensive_results(foo, bar) # Initializes VeryExpensiveObject and calls 'get_expensive_results' on it, passing in foo and bar
class LazyObject < BasicObject
def self.version
- '0.1.0'
+ '0.2.0'
end
def initialize(&callable)
@__callable__ = callable
end
@@ -29,10 +29,14 @@
!__target_object__
end
# Cached target object.
def __target_object__
- @__target_object__ ||= @__callable__.call
+ if defined?(@__target_object__)
+ @__target_object__
+ else
+ @__target_object__ = @__callable__.call
+ end
end
# Forwards all method calls to the target object.
def method_missing(method_name, ...)
__target_object__.send(method_name, ...)