Class: Array

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

Instance Method Summary collapse

Instance Method Details

#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

#to_bObject

Returns true if not empty.



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

def to_b
	!empty?
end