Class: Array

Inherits:
Object show all
Defined in:
lib/mug/top.rb,
lib/mug/bool.rb,
lib/mug/array/minus.rb,
lib/mug/array/extend.rb

Instance Method Summary collapse

Instance Method Details

#bottom!(n = 1, &blk) ⇒ Object



104
105
106
# File 'lib/mug/top.rb', line 104

def bottom! n=1, &blk
	replace(bottom n, &blk)
end

#bottom_by!(n = 1, &blk) ⇒ Object



107
108
109
110
# File 'lib/mug/top.rb', line 107

def bottom_by! n=1, &blk
	return enum_for(:bottom_by!, n) unless block_given?
	replace(bottom_by n, &blk)
end

#extend(*args, &block) ⇒ Object

See Also:



48
49
50
# File 'lib/mug/array/extend.rb', line 48

def extend *args, &block
	dup.extend!( *args, &block )
end

#extend!(size, *rest) ⇒ Object

Extend this Array.

In the first form, when a size and an optional obj are sent, the array is extended with size copies of obj. Take notice that all elements will reference the same object obj.

The second form appends a copy of the array passed as a parameter (the array is generated by calling #to_ary on the parameter). In the last form, the array is extended by the given size. Each new element in the array is created by passing the element’s index to the given block and storing the return value.

@call-seq extend!(size=0, obj=nil) @call-seq extend!(array) @call-seq extend!(size) {|index| block }

Raises:

  • (ArgumentError)

See Also:

  • #concat
  • #+


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mug/array/extend.rb', line 24

def extend! size, *rest
	raise ArgumentError, "wrong number of arguments (#{rest.length+1} for 1..2)" if rest.length > 1

	# Same logic as array.c/rb_ary_initialize
	if rest.empty? && !size.is_a?(Fixnum)
		warn 'warning: given block not used' if block_given?
		concat size.to_ary
		return self
	end

	raise ArgumentError, 'negative size' if size < 0

	a = length
	b = a+size
	if block_given?
		warn 'warning: block supersedes default value argument' if !rest.empty?
		fill(a...b) {|i| yield i }
	else
		obj = rest[0]
		fill(a...b) { obj }
	end
end

#minus(ary, remainder: false) ⇒ Object

Subtract elements from this array.

This is similar to Array#- except that elements from this array are removed only once per instance in ary.

If remainder is given and true, returns a second array which is all elements in ary that were not present in this array.

@call-seq minus(ary) @call-seq minus(ary, remainder: true)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mug/array/minus.rb', line 16

def minus ary, remainder: false

	result = dup
	rem = []

	ary.each do |x|
		i = result.index x
		if i
			result.delete_at i
		elsif remainder
			rem << x
		end
	end

	if remainder
		[result, rem]
	else
		result
	end
end

#to_bObject

Converts ary to a boolean. Returns true if not empty.



66
67
68
# File 'lib/mug/bool.rb', line 66

def to_b
	!empty?
end

#top!(n = 1, &blk) ⇒ Object



97
98
99
# File 'lib/mug/top.rb', line 97

def top! n=1, &blk
	replace(top n, &blk)
end

#top_by!(n = 1, &blk) ⇒ Object



100
101
102
103
# File 'lib/mug/top.rb', line 100

def top_by! n=1, &blk
	return enum_for(:top_by!, n) unless block_given?
	replace(top_by n, &blk)
end