Sha256: e4f2a64376ca8fa75a74beb3372f79c543bc682e466228acc9c55dda2bf01158

Contents?: true

Size: 458 Bytes

Versions: 2

Compression:

Stored size: 458 Bytes

Contents

class Array

	# Returns the values in common for an array set (nil, single value/object, or range).
  #
  # CREDIT: monocle

	def intersection
		array = uniq.sort_by { |e| Range === e ? e.first : e }
		array.inject() do |c, e|
			f = (Range === c ? c.last  : c)
			v = (Range === e ? e.first : e)
			case f <=> v
			when -1 then return nil
			when  0 then f
			else
				if Range === e
					v..([f, e.last].min)
				else
					v
				end
			end
		end
	end

end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/array/intersection.rb
facets-3.1.0 lib/core/facets/array/intersection.rb