Module: Linguistics::ArrayExtensions

Included in:
Array
Defined in:
lib/linguistics/monkeypatches.rb

Overview

A collection of extensions that get added to Array.

Instance Method Summary collapse

Instance Method Details

#separate(*args, &block) ⇒ Object

Returns a new Array that has had a new member inserted between all of the current ones. The value used is the given value argument unless a block is given, in which case the block is called once for each pair of the Array, and the return value is used as the separator.



12
13
14
15
16
# File 'lib/linguistics/monkeypatches.rb', line 12

def separate( *args, &block )
	ary = self.dup
	ary.separate!( *args, &block )
	return ary
end

#separate!(*args) ⇒ Object

The same as #separate, but modifies the Array in place.

Raises:

  • (LocalJumpError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/linguistics/monkeypatches.rb', line 19

def separate!( *args )
	raise LocalJumpError, "no block given for no-arg #separate!" if
		args.empty? && !block_given?
	value = args.first

	(1..( (self.length * 2) - 2 )).step(2) do |i|
		if block_given?
			self.insert( i, yield(self[i-1,2]) )
		else
			self.insert( i, value )
		end
	end
	self
end