Class: Ludy::Lazy
- Inherits:
-
Object
- Object
- Ludy::Lazy
- Defined in:
- lib/ludy/lazy.rb
Overview
a lazy object would be evaluated until the time it called, and save the result for futher use.
Instance Method Summary collapse
-
#initialize(func = nil, &block) ⇒ Lazy
constructor
supply the evaluation function through first argument or block.
-
#method_missing(msg, *arg, &block) ⇒ Object
:nodoc:.
Constructor Details
#initialize(func = nil, &block) ⇒ Lazy
supply the evaluation function through first argument or block
13 14 15 16 17 18 19 |
# File 'lib/ludy/lazy.rb', line 13 def initialize func = nil, &block if block_given? then @func = block else raise TypeError, "#{func} don't respond to :call" unless func.respond_to? :call @func = func end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(msg, *arg, &block) ⇒ Object
:nodoc:
22 23 24 |
# File 'lib/ludy/lazy.rb', line 22 def method_missing msg, *arg, &block (@obj ||= @func.call).public_send msg, *arg, &block end |