module RandomAccessible::CommonTraits

RandomAccessible::CommonTraits mixin provides methods commonly used for read and write access.

Public Instance Methods

each_index(&block) click to toggle source

Same as Array’s. This method evaluates no element of the class. This method raises NotImplementedError if the class provides neither size nor length method.

# File lib/common-traits.rb, line 16
def each_index(&block)
  if block.nil?
    Enumerator.new do |y|
      size.times do |i|
        y << i
      end
    end
  else
    size.times do |i|
      block.call(i)
    end
  end
end
empty?() click to toggle source

Same as Array’s. This method evaluates no element of the class. This method raises NotImplementedError if the class provides neither size nor length method.

# File lib/common-traits.rb, line 34
def empty?
  if has_size?
    size <= 0
  else
    false
  end
end
length() click to toggle source

This method is a size-provider (see README). Overriding method returns the number of the elements.

# File lib/common-traits.rb, line 61
def length
  if method(:size).owner == CommonTraits
    raise NotImplementedError,
          "#{self.class.to_s} overrides neither length method nor size method."
  end
  return size
end
size() click to toggle source

This method is a size-provider (see README). Overriding method returns the number of the elements.

# File lib/common-traits.rb, line 51
def size
  if method(:length).owner == CommonTraits
    raise NotImplementedError,
          "#{self.class.to_s} overrides neither length method nor size method."
  end
  return length
end

Private Instance Methods

has_size?() click to toggle source

Returns true if the object implements size or length method.

# File lib/common-traits.rb, line 43
def has_size?
  return method(:size).owner != CommonTraits ||
         method(:length).owner != CommonTraits
end