From: toyofuku@... Date: 1999-10-25T03:42:52+09:00 Subject: [ruby-list:17970] Re: <=> 豊福です。 私 >> bar.sort!{|a,b| (a.to_i <=> b.to_i).nonzero? || a.to_s <=> b.to_s } > ... >> bar.filter{|i| [i.to_i, i.to_s, i] }.sort!.filter{|i| i.pop } > > bar.filter{|i| [i, nil, nil] }.sort!{|a,b| > ((a[1] || (a[1] = a[0].to_i)) <=> (b[1] || (b[1] = b[0].to_i))).nonzero? || > ((a[2] || (a[2] = a[0].to_s)) <=> (b[2] || (b[2] = b[0].to_s))) > }.filter{|i| i[0] } sort! にブロックを渡しているのがしゃくだったので次の ような変態的コードを書いてみました。 procedure を call する適当なタイミングがわからなかった ので無理やり <=> のときに呼んでます。 class String alias cmp <=> def <=>(other); self.cmp((Proc === other) ? other.call: other); end end class Proc def <=>(other); self.call <=> ((Proc === other) ? other.call: other); end end bar.filter{|i| a = [i.to_i, nil, i]; a[1] = proc{a[1] = a[2].to_s}; a }.sort!.filter{|i| i.pop } うまい書き方があったら教えて下さい。 なんか bar.filter{|i| [i.to_i, once{|a| a[1] = a[2].to_s}, i] }.sort!.filter{|i| i.pop } とか bar.filter{|i| [i.to_i, once{i.to_s}, i] }.sort!.filter{|i| i.pop } みたいなことできないですかね。 --- 豊福 toyofuku@juice.or.jp