Class: CLASP::Util::ImmutableArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/clasp/util/immutable_array.rb

Overview

An immutable array

Direct Known Subclasses

Arguments::ImmutableArray

Instance Method Summary collapse

Constructor Details

#initialize(a) ⇒ ImmutableArray

:nodoc:

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
# File 'lib/clasp/util/immutable_array.rb', line 64

def initialize(a)

  raise ArgumentError, "must supply array" if a.nil?
  raise TypeError, "must supply instance of #{::Array}; #{a.class} given" unless a.is_a? ::Array

  @a = a
end

Instance Method Details

#==(rhs) ⇒ Object

Determines whether rhs is each to the receiver



129
130
131
132
133
134
# File 'lib/clasp/util/immutable_array.rb', line 129

def == rhs

  return rhs == @a if rhs.is_a? self.class

  @a == rhs
end

#[](*args) ⇒ Object

Same semantics as Array#[]



123
124
125
126
# File 'lib/clasp/util/immutable_array.rb', line 123

def [] *args

  slice(*args)
end

#eachObject

Calls the block once for each element in the array



73
74
75
76
77
78
# File 'lib/clasp/util/immutable_array.rb', line 73

def each

  return @a.each unless block_given?

  @a.each { |i| yield i }
end

#empty?Boolean

Indicates whether the immutable array has no elements

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/clasp/util/immutable_array.rb', line 87

def empty?

  @a.empty?
end

#find(ifnone = nil) ⇒ Object

Same semantics as Enumerable#find for the underlying array



93
94
95
96
97
98
# File 'lib/clasp/util/immutable_array.rb', line 93

def find ifnone = nil

  return @a.find(ifnone) { |o| yield o } if block_given?

  @a.find ifnone
end

#lengthObject

The number of elements in the immutable array



101
102
103
104
# File 'lib/clasp/util/immutable_array.rb', line 101

def length

  @a.length
end

#sizeObject

Alias for length



81
82
83
84
# File 'lib/clasp/util/immutable_array.rb', line 81

def size

  @a.size
end

#slice(*args) ⇒ Object

Same semantics as Array#slice



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/clasp/util/immutable_array.rb', line 107

def slice *args

  case args.length
  when 1
    case args[0]
    when ::Integer
      return @a.slice args[0]
    end
  when 2
  else
  end

  self.class.new @a.slice(*args)
end