Class: Literal::Array

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/literal/array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, type:) ⇒ Array

Returns a new instance of Array.



6
7
8
9
# File 'lib/literal/array.rb', line 6

def initialize(value, type:)
	@value = value
	@type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/literal/array.rb', line 12

def type
  @type
end

#value=(value) ⇒ Object

Sets the attribute value

Parameters:

  • value

    the value to set the attribute value to.



11
12
13
# File 'lib/literal/array.rb', line 11

def value=(value)
  @value = value
end

Instance Method Details

#&(other) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/literal/array.rb', line 158

def &(other)
	case other
	when Literal::Array(@type)
		new(@value & other.value)
	else
		@value & other
	end
end

#*(other) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/literal/array.rb', line 130

def *(other)
	case output = @value * other
	when Array
		new(output)
	when String
		output
	else
		raise ArgumentError
	end
end

#+(other) ⇒ Object



113
114
115
116
117
# File 'lib/literal/array.rb', line 113

def +(other)
	duplicate = dup
	duplicate.concat(other)
	duplicate
end

#-(other) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/literal/array.rb', line 119

def -(other)
	case other
	when Array
		Literal::Array.new(@value - other, type: @type)
	when Literal::Array
		Literal::Array.new(@value - other.value, type: @type)
	else
		raise ArgumentError
	end
end

#<<(item) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/literal/array.rb', line 40

def <<(item)
	unless @type === item
		raise Literal::TypeError.expected(item, to_be_a: @type)
	end

	@value << item
	self
end

#==(other) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/literal/array.rb', line 102

def ==(other)
	case other
	when Array
		@value == other
	when Literal::Array
		@value == other.value
	else
		false
	end
end

#[](index) ⇒ Object



57
58
59
# File 'lib/literal/array.rb', line 57

def [](index)
	@value[index]
end

#[]=(index, value) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/literal/array.rb', line 49

def []=(index, value)
	unless @type === value
		raise Literal::TypeError.expected(value, to_be_a: @type)
	end

	@value[index] = value
end

#append(*elements) ⇒ Object Also known as: push



72
73
74
75
76
77
78
79
# File 'lib/literal/array.rb', line 72

def append(*elements)
	case elements
	when Literal::_Array(@type)
		@value.append(*elements)
	else
		raise Literal::TypeError.expected(elements, to_be_a: Literal::_Array(@type))
	end
end

#at(i) ⇒ Object



263
264
265
# File 'lib/literal/array.rb', line 263

def at(i)
	@value.at(i)
end

#clearObject



167
168
169
170
# File 'lib/literal/array.rb', line 167

def clear
	@value.clear
	self
end

#compact!Object



210
211
212
213
# File 'lib/literal/array.rb', line 210

def compact!
	@value.compact!
	self
end

#concat(other) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/literal/array.rb', line 87

def concat(other)
	case other
	when Literal::Array(@type)
		@value.concat(other.value)
	when Literal::_Array(@type)
		@value.concat(other)
	when Literal::Array
		raise Literal::TypeError.expected(other, to_be_a: Literal::Array(@type))
	when Array
		raise Literal::TypeError.expected(other, to_be_a: Literal::_Array(@type))
	else
		raise ArgumentError
	end
end

#deleteObject



180
181
182
# File 'lib/literal/array.rb', line 180

def delete(...)
	@value.delete(...)
end

#dupObject



83
84
85
# File 'lib/literal/array.rb', line 83

def dup
	super.tap { |d| d.value = @value.dup }
end

#eachObject



18
19
20
# File 'lib/literal/array.rb', line 18

def each(&)
	@value.each(&)
end

#empty?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/literal/array.rb', line 231

def empty?
	@value.empty?
end

#fetchObject



267
268
269
# File 'lib/literal/array.rb', line 267

def fetch(...)
	@value.fetch(...)
end

#first(n = nil) ⇒ Object



251
252
253
# File 'lib/literal/array.rb', line 251

def first(n = nil)
	n ? new(@value.first(n)) : @value.first
end

#joinObject



255
256
257
# File 'lib/literal/array.rb', line 255

def join(...)
	@value.join(...)
end

#last(n = nil) ⇒ Object



247
248
249
# File 'lib/literal/array.rb', line 247

def last(n = nil)
	n ? new(@value.last(n)) : @value.last
end

#lengthObject



172
173
174
# File 'lib/literal/array.rb', line 172

def length
	@value.length
end

#map(type = @type) ⇒ Object



22
23
24
# File 'lib/literal/array.rb', line 22

def map(type = @type, &)
	Literal::Array(@type).new(@value.map(&))
end

#map!(type = @type, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/literal/array.rb', line 26

def map!(type = @type, &block)
	@value.map! do |item|
		output = block.call(item)

		if type === output
			output
		else
			raise Literal::TypeError.expected(output, to_be_a: type)
		end
	end

	@type = type
end

#new(value) ⇒ Object (private)



294
295
296
# File 'lib/literal/array.rb', line 294

private def new(value)
	Literal::Array.new(value, type: @type)
end

#packObject



259
260
261
# File 'lib/literal/array.rb', line 259

def pack(...)
	@value.pack(...)
end

#pop(n = nil) ⇒ Object



239
240
241
# File 'lib/literal/array.rb', line 239

def pop(n = nil)
	n ? new(@value.pop(n)) : @value.pop
end

#prepend(*elements) ⇒ Object Also known as: unshift



61
62
63
64
65
66
67
68
# File 'lib/literal/array.rb', line 61

def prepend(*elements)
	case elements
	when Literal::_Array(@type)
		@value.prepend(*elements)
	else
		raise Literal::TypeError.expected(elements, to_be_a: Literal::_Array(@type))
	end
end

#reject!Object



202
203
204
# File 'lib/literal/array.rb', line 202

def reject!(...)
	self if @value.reject!(...)
end

#reverseObject



223
224
225
# File 'lib/literal/array.rb', line 223

def reverse
	new @value.reverse
end

#reverse!Object



227
228
229
# File 'lib/literal/array.rb', line 227

def reverse!
	self if @value.reverse!
end

#rotate!Object



188
189
190
# File 'lib/literal/array.rb', line 188

def rotate!(...)
	self if @value.rotate!(...)
end

#sample(n = nil, random: nil) ⇒ Object



243
244
245
# File 'lib/literal/array.rb', line 243

def sample(n = nil, random: nil)
	n ? new(@value.sample(n, random:)) : @value.sample(random:)
end

#select!Object Also known as: filter!



196
197
198
# File 'lib/literal/array.rb', line 196

def select!(...)
	self if @value.select!(...)
end

#shift(n = nil) ⇒ Object



235
236
237
# File 'lib/literal/array.rb', line 235

def shift(n = nil)
	n ? new(@value.shift(n)) : @value.shift
end

#shuffleObject



215
216
217
# File 'lib/literal/array.rb', line 215

def shuffle(...)
	new @value.shuffle(...)
end

#shuffle!Object



219
220
221
# File 'lib/literal/array.rb', line 219

def shuffle!(...)
	self if @value.shuffle!(...)
end

#sizeObject



176
177
178
# File 'lib/literal/array.rb', line 176

def size
	@value.size
end

#sort!Object



184
185
186
# File 'lib/literal/array.rb', line 184

def sort!
	self if @value.sort!
end

#sort_by!Object



192
193
194
# File 'lib/literal/array.rb', line 192

def sort_by!(...)
	self if @value.sort_by!(...)
end

#union(*others) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/literal/array.rb', line 271

def union(*others)
	others.map! do |other|
		case other
		when Literal::Array
			if @type == other.type
				other.value
			else
				raise Literal::TypeError.expected(other, to_be_a: Literal::Array(@type))
			end
		when Array
			if Literal::_Array(@type) === other
				other
			else
				raise Literal::TypeError.expected(other, to_be_a: Literal::_Array(@type))
			end
		else
			raise ArgumentError
		end
	end

	new(@value.union(*others))
end

#uniq!Object



206
207
208
# File 'lib/literal/array.rb', line 206

def uniq!(...)
	self if @value.uniq!(...)
end

#|(other) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/literal/array.rb', line 141

def |(other)
	case other
	when Literal::Array
		if @type == other.type
			new(@value | other.value)
		else
			raise Literal::TypeError.expected(other, to_be_a: Literal::Array(@type))
		end
	when Array
		if Literal::_Array(@type) === other
			new(@value | other)
		else
			raise Literal::TypeError.expected(other, to_be_a: Literal::_Array(@type))
		end
	end
end