# an approach
class Array
private
def _uniq_by(&block)
new, memo = [], {}
map\(&block\)\.each\_with\_index do |mapped, idx|
unless memo\[mapped\]
memo\[mapped\] = true
new << self\[idx\]
end
end
new
end
alias_method :_uniq, :uniq
alias_method :_uniq!, :uniq!
public
def uniq(&block)
block_given? ? _uniq_by(&block) : _uniq
end
def uniq!(&block)
if block_given?
new = _uniq_by(&block)
if self == new
nil
else
replace\(new\)
end
else
\_uniq\!
end
end
end
# 1.9.2 original
# same return from 1.8.6, 1.8.7, 1.9.2, 1.9.3
···
--
Posted via http://www.ruby-forum.com/.