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.



1706
1707
1708
1709
1710
# File 'lib/linguistics/en.rb', line 1706

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)


1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
# File 'lib/linguistics/en.rb', line 1713

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