From: Michael DeHaan Date: 2004-11-10T06:41:08+09:00 Subject: Re: Iterating trough hash Hmm, that's not a good bug there, so I'll pull the "history" thing out (I was trying to be too clever)... module Enumerable def each_permutation each do |item1| each do |item2| yield [item1,item2] unless (item1 === item2) end end end end On Tue, 9 Nov 2004 16:34:55 -0500, Michael DeHaan wrote: > This is a really long one, so (being new to the language), I'd > appreciate feedback. Note here we don't print dups, and we also can > provide a custom definition of whether two people are equal...However > it doesn't use a hash quite as the OP requested :) > > module Enumerable > def each_permutation > history = Hash.new() > each do |item1| > each do |item2| > history[item1] = 1 > yield [item1,item2] unless (item1 === item2 || history.has_key?(item2)) > end > end > end > end > > class Person > attr_reader :age > attr_reader :name > > def initialize(name,age) > @name=name; > @age=age; > end > > def ===(other) > return ((@age==other.age())&&(@name==other.name())) > end > > def to_s > @name > end > > end > > @persons = [ > Person.new('Timmy',5), > Person.new('Jimmy',10), > Person.new('Sally',40) > ] > > @persons.each_permutation do | p1, p2 | > puts "#{p1} shakes hands with #{p2}. Together they are > #{p1.age()+p2.age()} years old." > end > > On Wed, 10 Nov 2004 05:23:39 +0900, ara.t.howard@noaa.gov > > > wrote: > > On Wed, 10 Nov 2004, Mark Hubbart wrote: > > > > > > > > >> if h.sort.index([person1,h[person1]]) < h.sort.index([person2,h[person2]]) > > >> > > >> says: > > >> > > >> if the name of person1 is less than the name of person2 OR the names are the > > >> SAME but the age of person1 is less than the age of person2. > > > > > > Um, no, I think this says: > > > > > > if the index of the first element that matches [person1, age1] is > > > greater than the > > > index of the first element that matches [person2, age2]. > > > > consider the meaning of the h.sort and how ruby will do it by default. > > > > h.sort => array of key value pairs sorted first on name then on age > > > > h.sort.index => look for the natural ordering of an entry where the > > ordering has been defined by ruby's Array#sort method > > > > you are correct in what you said - but what does that __mean__? if you think > > of what would cause an index to be less than the index of another entry you'll > > see what i mean: a particular key/val pair will only have a lesser index in > > the array produced by h.sort if either the name in the entry sorts first or > > the names are the same and the age sorts first. as you pointed out the names > > cannot be the same since the data is stored in a hash - so why include age in > > the search for ordering index when it can never be a tie breaker? eg this > > code would have the same effect > > > > if h.keys.sort.index(person1) < h.keys.sort.index(person2) > > > > my concern was that the OP included the age in the ordering search thinking it > > had some effect - it doesn't. in fact dermining the name's (or name/age pair) > > index into an ordered list is exactly the same as simply comparing names isn't > > it: > > > > harp:~ > cat a.rb > > h = {"john" => 41, "mary" => 31, "fred" => 10} > > > > puts '---' > > h.each_key do |person1| > > h.each_key do |person2| > > > > if h.sort.index([person1,h[person1]]) > > > age=(h[person1]+h[person2]).to_s > > puts "#{person1} shakes hands with #{person2}. Together they are #{age} years old" > > end > > end > > end > > > > puts '---' > > h.each_key do |person1| > > h.each_key do |person2| > > > > if person1 < person2 # <-- > > > > age=(h[person1]+h[person2]).to_s > > puts "#{person1} shakes hands with #{person2}. Together they are #{age} years old" > > end > > end > > end > > > > harp:~ > ruby a.rb > > --- > > john shakes hands with mary. Together they are 72 years old > > fred shakes hands with john. Together they are 51 years old > > fred shakes hands with mary. Together they are 41 years old > > --- > > john shakes hands with mary. Together they are 72 years old > > fred shakes hands with john. Together they are 51 years old > > fred shakes hands with mary. Together they are 41 years old > > > > so - why do you think the OP included age in the search? what was the code > > trying to say? > > > > kind regards. > > > > > > > > -a > > -- > > =============================================================================== > > | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov > > | PHONE :: 303.497.6469 > > | When you do something, you should burn yourself completely, like a good > > | bonfire, leaving no trace of yourself. --Shunryu Suzuki > > =============================================================================== > > > > >