[#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:39829] Re: (要素がString, Fixnum 以外の)配列の集合演算

From: Hiroshi Takagi <gollum@...>
Date: 2004-07-06 06:34:50 UTC
List: ruby-list #39829
高木です。

解読に時間がかかりました。


On Tue, 6 Jul 2004 09:17:04 +0900
nobu.nakada@nifty.ne.jp wrote:


> > うう、この二つが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

include してうまく行きました。

Object#instance_variables
Object#instance_variable_get
Enumerable#inject
Enumerable#all? { }

と、1.8 feature てんこもりですね :-)
勉強になりました、ありがとうございます。

一点、わからないのが、

  (v = nil; oh = {}; ov.each {|v| oh[v] = true}; sv).all? do |v|

の部分です。

ここは、

sv.all? do |v| ... end

だけでは、まずいことがおこるのでしょうか?

-- 
Hiroshi Takagi <gollum@hi-net.zaq.ne.jp>



In This Thread