Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/linguistics/en.rb

Overview

Add the #separate and #separate! methods to Array.

Instance Method Summary collapse

Instance Method Details

#separate(value = :__no_arg__, &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.



1672
1673
1674
1675
1676
# File 'lib/linguistics/en.rb', line 1672

def separate( value=:__no_arg__, &block )
	ary = self.dup
	ary.separate!( value, &block )
	return ary
end

#separate!(value = :__no_arg__) ⇒ Object

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

Raises:

  • (ArgumentError)


1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
# File 'lib/linguistics/en.rb', line 1679

def separate!( value=:__no_arg__ )
	raise ArgumentError, "wrong number of arguments: (0 for 1)" if
		value == :__no_arg__ && !block_given?

	(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