From: "Aleksi Niemelä" Date: 2000-11-09T05:35:57+09:00 Subject: [ruby-talk:6178] Re: detect:ifNone: in Ruby Hugh asks: > So I'd have > print myarray.sort!.when(nil,myarray).uniq!.when(nil,myarray).inspect > you mean? Well, that was not what I was thinking.... I was actually just going to reply to you that your problem seems quite hopeless to me. I was almost going to use word "impossible" when I realized that by changing the problem a little (changing cut from trailing position into prefixing one) we might be able to pull something out... So keep doing it in the method you describe above if you want, but here is a way couple characters and one repetitive piece of code less. :) Even with this workaround, IMHO 'method!' returning anything but it's receiver is not good design. And I predict it will continue to suprise many forecoming generations. - Aleksi require 'when' class Object def cut Cut_.new(self) end end class Cut_ def initialize(obj) @obj = obj end def method_missing(name, *args) @obj.method(name).call(*args).when nil, @obj end end [ [1,1,2,2,3], [1,2,3], ].each do |ary| a = ary.dup puts "not cut: ", a.sort!.uniq!.inspect a = ary.dup puts " cutted: ", a.sort!.cut.uniq!.inspect # just to demonstrate Object#when for the same a = ary.dup puts " whened: ", a.uniq!.when(nil,a).inspect puts end And it's output: not cut: [1, 2, 3] cutted: [1, 2, 3] whened: [1, 2, 3] not cut: nil cutted: [1, 2, 3] whened: [1, 2, 3]