class Abstractivator::Lazy

A transparent wrapper for delaying evaluation. Usage: v = lazy { 1 + 2 } This delays the addition until the value is actually needed, i.e., a method is called on it. The wrapper is “transparent” in that it acts as much as possible like the wrapped object itself. For example: lazy { 42 }.class ==> Fixnum Don't use this for mission critical code. Don't pass lazy objects across API boundaries.

Public Class Methods

new(&make) click to toggle source
# File lib/abstractivator/lazy.rb, line 14
def initialize(&make)
  @make = make
end

Public Instance Methods

!() click to toggle source
# File lib/abstractivator/lazy.rb, line 39
def !
  !__lazy_ensure_obj
end
!=(other) click to toggle source
# File lib/abstractivator/lazy.rb, line 35
def !=(other)
  __lazy_ensure_obj != other
end
==(other) click to toggle source
# File lib/abstractivator/lazy.rb, line 31
def ==(other)
  __lazy_ensure_obj == other
end
inspect() click to toggle source
# File lib/abstractivator/lazy.rb, line 72
def inspect
  "#<Abstractivator::Lazy:#{@make ? '' : @obj.class.name + ':'}0x#{__id__.to_s(16).rjust(8, '0')}>"
end
method_missing(method, *args, **kws, &block) click to toggle source
# File lib/abstractivator/lazy.rb, line 27
def method_missing(method, *args, **kws, &block)
  __lazy_ensure_obj.proxy_send(method, *args, **kws, &block)
end
to_a() click to toggle source

Force ruby to delegate to wrapped object. Ruby's C implementation makes assumptions about object type. This is required for explicit array coercion:

x = 1
a = *x
(a == [1])
# File lib/abstractivator/lazy.rb, line 63
def to_a
  __lazy_ensure_obj
  if @obj.respond_to?(:to_a)
    @obj.to_a
  else
    nil
  end
end
to_ary() click to toggle source

Force ruby to delegate to wrapped object. Ruby's C implementation makes assumptions about object type. This is required for implicit array coercion:

a = [1, 2]
x, y = a
# File lib/abstractivator/lazy.rb, line 48
def to_ary
  __lazy_ensure_obj
  if @obj.respond_to?(:to_ary)
    @obj.to_ary
  else
    nil
  end
end

Private Instance Methods

__lazy_ensure_obj() click to toggle source
# File lib/abstractivator/lazy.rb, line 18
def __lazy_ensure_obj
  if @make
    @obj = @make.call
    @make = nil
  end
  @obj
end