For times when we care about the uniqueness or compactness of the elements
of the array, we could have
uniq?
compact?
I like this…
if you plan on doing more than just calling uniq! or compact! anyway.
Other ideas:
- change ! methods to always return the receiver, create new methods
that have the current ! functionality:
This would break existing code possibly.
I wonder about using an optional parameter to return status
information, instead of having to “overload” the method’s result …
Script started on Thu Feb 13 16:43:44 2003
neelix hgs 11 %> cat test_new_uniq.rb
#!/usr/local/bin/ruby -w
class BoolStatus
def initialize(bool)
@value = bool
end
def true
@value = true
end
def false
@value = false
end
def true?
@value == true ? true : false
end
def false?
@value == false ? true : false
end
def to_s
@value.to_s
end
end
class Array
alias old_uniq! uniq!
def uniq!(modified=nil)
if old_uniq!.nil?
if modified.nil?
return nil # same as now
else
modified.false
end
else
unless modified.nil?
modified.true
end
end
return self
end
end
u = [1,2,3,4,5]
v = [1,2,3,4,5]
x = [1,2,2,3,3,4,4,5]
y = [1,2,2,3,3,4,4,5]
z = BoolStatus.new(false)
puts u.uniq!.inspect
puts v.uniq!(z).inspect
puts z.inspect
puts x.uniq!.inspect
puts y.uniq!(z).inspect
puts z.inspect
neelix hgs 12 %> ./test_new_uniq/.rb
nil
[1, 2, 3, 4, 5]
#<BoolStatus:0xe8f98 @value=false>
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
#<BoolStatus:0xe8f98 @value=true>
neelix hgs 13 %> exit
neelix hgs 14 %>
script done on Thu Feb 13 16:44:05 2003
Having the extra parameter to determine behaviour desn’t break
existing code, I think.
Jim Hranicky, Senior SysAdmin UF/CISE Department |
Hugh
···
On Thu, 13 Feb 2003, James F.Hranicky wrote: