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.



1728
1729
1730
1731
1732
# File 'lib/linguistics/en.rb', line 1728

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)


1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
# File 'lib/linguistics/en.rb', line 1735

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