[#39863] forループの速度 — Masahiro Sato <msato@...>

15 messages 2004/07/20

[#39868] イテレータとfor文 — OOTANI TAKASHI <otn@...5.so-net.ne.jp>

大谷と申します。

31 messages 2004/07/20
[#39886] Re: イテレータとfor文 — Tietew <tietew-ml-ruby-list@...> 2004/07/21

[ruby-list:39826] Re: (要素がString, Fixnum 以外の)配列の集合演算

From: nobu.nakada@...
Date: 2004-07-06 00:17:04 UTC
List: ruby-list #39826
なかだです。

At Tue, 6 Jul 2004 00:32:53 +0900,
Hiroshi Takagi wrote in [ruby-list:39824]:
> >          def eql?(other)
> >            @attr1.eql?(other.attr1) && @attr2.eql?(other.attr2)
> >          end
> >          def hash
> >            @attr1.hash + @attr2.hash
> >          end
> 
> def hash の方は、いろいろな定義ができると思いますが、
> def eql?(other) の方は、いつもこういうパターンになりますね。
> 
> うう、この二つがMix in できるような方法はないものか...

こんな感じでどうでしょうか。

  module Hash::ReadyMade
    def eql?(other)
      self.class == other.class and
      (sv = instance_variables).size == (ov = other.instance_variables).size and
      (v = nil; oh = {}; ov.each {|v| oh[v] = true}; sv).all? do |v|
        ov.include?(v) and
          instance_variable_get(v).eql?(other.instance_variable_get(v))
      end
    end
    def hash
      instance_variables.inject(self.class.hash) {|h, v| h ^ instance_variable_get(v).hash}
    end
  end

-- 
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
    中田 伸悦

In This Thread