Module: RandomAccessible::CommonTraits

Included in:
RandomReadable, RandomWritable
Defined in:
lib/common-traits.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#each_index(&block) ⇒ Object

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.



16
17
18
19
20
21
22
23
24
25
26
27
28
# 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? ⇒ Boolean

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.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/common-traits.rb', line 34

def empty?
  if has_size?
    size <= 0
  else
    false
  end
end

#length ⇒ Object

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



61
62
63
64
65
66
67
# 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 ⇒ Object

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



51
52
53
54
55
56
57
# 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