Top Level Namespace

Defined Under Namespace

Modules: ArraySentence, ModNormalizedHash, NormalizedArrayMethods, RFC822 Classes: Doodle, NormalizedArray, NormalizedHash, StringArray, StringHash, StringKeyHash, SymbolKeyHash

Constant Summary collapse

StringIntegerHash =
NormalizedHash::MultiTypedHash(String, Integer)
ReverseStringHash =
NormalizedHash::MultiTypedHash() do |v|
  if v.kind_of?(String)
    v.reverse
  else
    v
  end
end
TypedStringArray =
TypedArray(String)
BoundedArray4 =
BoundedArray(4)
TypedIntegerArray =

ca = BoundedArray.new()

pp ca
TypedArray(Integer)
StringOrIntegerArray =
TypedArray(Integer, String)

Instance Method Summary collapse

Instance Method Details

#BoundedArray(upper_bound) ⇒ Object



383
384
385
386
387
388
389
390
391
# File 'lib/doodle/normalized_array.rb', line 383

def BoundedArray(upper_bound)
  typed_class = Class.new(NormalizedArray) do
    define_method :normalize_index do |index|
      raise IndexError, "index #{index} out of range" if !(0..upper_bound).include?(index)
      index
    end
  end
  typed_class
end

#TypedArray(*klasses) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
# File 'lib/doodle/normalized_array.rb', line 393

def TypedArray(*klasses)
  typed_class = Class.new(NormalizedArray) do
    define_method :normalize_value do |v|
      if !klasses.any?{ |klass| v.kind_of?(klass) }
        raise TypeError, "#{self.class}: #{v.class}(#{v.inspect}) is not a kind of #{klasses.map{ |c| c.to_s }.join(', ')}", [caller[-1]]
      end
      v
    end
  end
  typed_class
end