Class: Array

Inherits:
Object show all
Defined in:
lib/tagen/core/array.rb,
lib/tagen/core/array/extract_options.rb

Overview

Additional method list


  • `#append` _alias from push_

Instance Method Summary (collapse)

Instance Method Details

- (Array) delete(*values, &blk)

support delete more than one values.

original: delete(v) currrent: delete(*v)

Returns:



20
21
22
23
24
25
# File 'lib/tagen/core/array.rb', line 20

def delete *values, &blk
	indexs = values.each.with_object [] do |v,m|
		m << original_delete(v, &blk)
	end
	values.length==1 ? indexs[0] : indexs
end

- (Array) delete_at(*indexs, &blk)

support delate_at more than one index.

original: delete_at(i) current: delte_at(*i)

Returns:



35
36
37
38
39
40
# File 'lib/tagen/core/array.rb', line 35

def delete_at *indexs, &blk
	values = indexs.each.with_object [] do |i,m|
		m << original_delete_at(i, &blk)
	end
	indexs.length==1 ? values[0] : values
end

- (Array<Object>, Hash) extract_extend_options(*defaults)

extend options are symbols and hash, symbol as a boolean option.

:a #=> { a: true }
:_a #=> { a: false}

Examples:

def foo(*args)
  paths, o = args.extract_extend_options
end

foo(1, :a, :_b, :c => 2) 
  #=> paths is [1]
  #=> o is {a: true, b: false, c: 2}

Parameters:

  • *defaults (Symbol, Hash)

Returns:



19
20
21
22
23
# File 'lib/tagen/core/array/extract_options.rb', line 19

def extract_extend_options *defaults
	args, o = _parse_o(defaults)
	args1, o1 = _parse_o(self)
	[args+args1, o.merge(o1)]
end

- (Hash) extract_extend_options!(*defaults)

modify args IN PLACE. @ see extract_extend_options

Examples:

def foo(*args)
  options = args.extract_extend_options!
end

foo(1, :a, :_b, c: 2)
  #=> args is [1]
  #=> o is {a: true, b: false, c:2}

Parameters:

  • *defaults (Symbol, Hash)

Returns:

  • (Hash)

    options



39
40
41
42
43
# File 'lib/tagen/core/array/extract_options.rb', line 39

def extract_extend_options! *defaults
	args, o = extract_extend_options *defaults
	self.replace args
	o
end

- (Array<Array,Hash>) extract_options(default = {})

extract options

Examples:

def mkdir(*args)
  paths, o = args.extract_options
end

Returns:

See Also:

  • extract_options!


96
97
98
99
100
101
102
# File 'lib/tagen/core/array/extract_options.rb', line 96

def extract_options default={}
	if self.last.is_a?(Hash) && self.last.instance_of?(Hash)
		[self[0...-1], self[-1].merge(default)]
	else
		[self, default]
	end
end

- (Hash) extract_options!(default = {})

Extracts options from a set of arguments. Removes and returns the last element in the array if it's a hash, otherwise returns a blank hash. you can also pass a default option.

Examples:

def options(*args)
  o = args.extract_options!(:a=>1)
end

options(1, 2)           # => {:a=>1}
options(1, 2, :a => :b) # => {:a=>:b}

Parameters:

  • default (Hash) (defaults to: {})

    default options

Returns:



80
81
82
83
84
85
86
# File 'lib/tagen/core/array/extract_options.rb', line 80

def extract_options! default={}
	if self.last.is_a?(Hash) && self.last.instance_of?(Hash)
		self.pop.merge default
	else
		default
	end
end

- (Object) find!(&blk)

same as find, but delete the finded value



76
77
78
79
80
81
82
83
# File 'lib/tagen/core/array.rb', line 76

def find! &blk
	idx = self.find_index(&blk)
	if idx
		self.delete_at(idx)
	else
		nil
	end
end

- (Object) find_all!(&blk)

same as find_all, but delete all finded values



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tagen/core/array.rb', line 86

def find_all! &blk
	ret, rest = [], []
	self.each do |k|
		if blk.call(k)
			ret << k
		else
			rest << k
		end
	end
	self.replace rest
	ret
end

- (Object) first!(n = 1)

Note:

modify IN PLACE

first n values, not works at Range, Range don't have #replace method

Examples:

a = %w(1 2 3)
a.first!(2)  #=> [1, 2]
a            #=> [3]
@return Array


51
52
53
54
55
56
57
58
59
# File 'lib/tagen/core/array.rb', line 51

def first! n=1
	i = 0 
	j = i + n
	k = -1

	ret = self[i...j]
	self.replace self[j..k]
	ret
end

- (Object) last!(n = 1)

last n values

See Also:

  • first!


63
64
65
66
67
68
69
70
71
# File 'lib/tagen/core/array.rb', line 63

def last! n=1
	i = -1 
	j = -1 - n + 1
	k = 0

	ret = self[j..i]
	self.replace self[k...j]
	ret
end

- (Object) original_delete



12
# File 'lib/tagen/core/array.rb', line 12

alias original_delete delete

- (Object) original_delete_at



27
# File 'lib/tagen/core/array.rb', line 27

alias original_delete_at delete_at