module

Wrapper(T)

Wraps an object such that methods are delegated to the wrapped object. The return values of the methods will be wrapped as well if the return type is the same as the wrapped object.

struct SpecialArray
  include Wrapper(Array(Int32))
  fully_delegate
end

arr = [1, 2, 3, 4]
sarr = SpecialArray.new arr
sarr[0]  # => 1
sarr[-1] # => 4

# Returned objects are wrapped if appropiate
sarr[0..2]           # => SpecialArray[1, 2, 3]
sarr.select(&.even?) # => SpecialArray[2, 4]

# Write access the original object
sarr.select!(&.odd?)
sarr # => SpecialArray[1, 3]
arr  # => [1, 3]

sarr.wrapped.same? arr # => true

Constructors

new(wrapped : T = T.new)
Source

Instance methods

==(rhs : T) : Bool
Source
==(rhs : self) : Bool
Source
pretty_print(*args, **options)
Source
pretty_print(*args, **options, &)
Source
to_s(*args, **options)
Source
to_s(*args, **options, &)
Source
wrapped

Returns the wrapped object.

Source

Macros

delegate(*methods)

Delegates methods to the enclosed object, wrapping the return value if it is of the same type as the enclosed object.

struct CustomInt
  include Wrapper(Int32)

  delegate :+
end

val = CustomInt.new(5) + 10
val.class # => CustomInt
val       # => 15
Source
fully_delegate

Delegates missing methods to the wrapped object using the method_missing hook.

Source