Class: Is::Lazy::Value
- Inherits:
- BasicObject
- Defined in:
- lib/is/lazy.rb
Overview
Object-placeholder for calculation.
Instance Method Summary collapse
-
#! ⇒ Boolean
Calculate the value and return it’s logical not.
-
#!=(other) ⇒ Boolean
Calculate value and compare it with other object.
-
#+@ ⇒ Object
Calculate and return the value.
-
#==(other) ⇒ Boolean
Calculate value and compare it with other object.
-
#equal?(other) ⇒ Boolean
Calculate value and compare it with other object.
-
#initialize(mode, *args, &block) ⇒ Value
constructor
A new instance of Value.
-
#method_missing(sym, *args, &block) ⇒ Object
Pass all unknown method calls to value.
-
#stop ⇒ Object?
Stop the calculation and return value immediatelly.
-
#to_ary ⇒ Array?
Used by interpreter for implicit convertion to Array.
-
#to_proc ⇒ Proc?
Used by interpreter for implicit convertion to Proc.
-
#to_regexp ⇒ Regexp?
Used by interpreter for implicit convertion to Regexp.
-
#to_str ⇒ String?
Used by interpreter for implicit convertion to String.
-
#to_sym ⇒ Symbol?
Used by interpreter for implicit convertion to Symbol.
-
#true? ⇒ Boolean
(also: #to_b)
Calculate value and convert it to boolean.
-
#value ⇒ Object
Calculate result value (or wait for thread) and return it.
Constructor Details
#initialize(mode, *args, &block) ⇒ Value
Returns a new instance of Value.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/is/lazy.rb', line 12 def initialize mode, *args, &block case mode when :lambda @args = args @lambda = lambda &block @mode = :lambda when :thread @thread = ::Thread.start *args, &block @mode = :thread else @value = block.call *args @mode = :value end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Pass all unknown method calls to value.
66 67 68 |
# File 'lib/is/lazy.rb', line 66 def method_missing sym, *args, &block self.value.send sym, *args, &block end |
Instance Method Details
#! ⇒ Boolean
Calculate the value and return it’s logical not.
72 73 74 |
# File 'lib/is/lazy.rb', line 72 def ! ! self.value end |
#!=(other) ⇒ Boolean
Calculate value and compare it with other object.
93 94 95 |
# File 'lib/is/lazy.rb', line 93 def != other self.value != other end |
#+@ ⇒ Object
Calculate and return the value. This is a shortest expression with value.
79 80 81 |
# File 'lib/is/lazy.rb', line 79 def +@ self.value end |
#==(other) ⇒ Boolean
Calculate value and compare it with other object.
86 87 88 |
# File 'lib/is/lazy.rb', line 86 def == other self.value == other end |
#equal?(other) ⇒ Boolean
Calculate value and compare it with other object.
100 101 102 |
# File 'lib/is/lazy.rb', line 100 def equal? other self.value.equal? other end |
#stop ⇒ Object?
Stop the calculation and return value immediatelly. If calculation was finished before, returns real value, else nil.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/is/lazy.rb', line 46 def stop case @mode when :lambda @args = nil @lambda = nil @value = nil when :thread if @thread.status == false @value = @thread.value else @thread.terminate @value = nil end @thread = nil end @mode = :value @value end |
#to_ary ⇒ Array?
Used by interpreter for implicit convertion to Array.
124 125 126 |
# File 'lib/is/lazy.rb', line 124 def to_ary self.value.to_ary end |
#to_proc ⇒ Proc?
Used by interpreter for implicit convertion to Proc.
130 131 132 |
# File 'lib/is/lazy.rb', line 130 def to_proc self.value.to_proc end |
#to_regexp ⇒ Regexp?
Used by interpreter for implicit convertion to Regexp.
136 137 138 |
# File 'lib/is/lazy.rb', line 136 def to_regexp self.value.to_regexp end |
#to_str ⇒ String?
Used by interpreter for implicit convertion to String.
114 115 116 117 118 119 120 |
# File 'lib/is/lazy.rb', line 114 def to_str if self.value.respond_to? :to_str self.value.to_str else self.value.to_s end end |
#to_sym ⇒ Symbol?
Used by interpreter for implicit convertion to Symbol.
142 143 144 |
# File 'lib/is/lazy.rb', line 142 def to_sym self.value.to_sym end |
#true? ⇒ Boolean Also known as: to_b
Calculate value and convert it to boolean.
106 107 108 |
# File 'lib/is/lazy.rb', line 106 def true? ! ! self.value end |
#value ⇒ Object
Calculate result value (or wait for thread) and return it.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/is/lazy.rb', line 29 def value case @mode when :lambda @value = @lambda.call *@args @lambda = nil @args = nil when :thread @value = @thread.value @thread = nil end @mode = :value @value end |