[#385051] How to order Structs based on two fields — Iñaki Baz Castillo <ibc@...>

Hi, I've a struct like this:

20 messages 2011/07/01
[#385052] Re: How to order Structs based on two fields — Iñaki Baz Castillo <ibc@...> 2011/07/01

2011/7/1 I=C3=B1aki Baz Castillo <ibc@aliax.net>:

[#385057] Re: How to order Structs based on two fields — Robert Klemme <shortcutter@...> 2011/07/01

On Fri, Jul 1, 2011 at 12:55 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrote=

[#385069] Offering Help — Joe Peric <peric.joe@...>

I'm new to Ruby and I am willing to learn. Anyone need help on a

18 messages 2011/07/01
[#385072] Re: Offering Help — Jeremy Heiler <jeremyheiler@...> 2011/07/01

On Fri, Jul 1, 2011 at 11:43 AM, Joe Peric <peric.joe@gmail.com> wrote:

[#385073] Re: Offering Help — Joe Peric <peric.joe@...> 2011/07/01

Never heard of it, but I'll get right on it. Thanks.

[#385082] Re: Offering Help — Joe Peric <peric.joe@...> 2011/07/02

Just finished this. I had to find some more info on exceptions to get

[#385083] Re: Offering Help — Jeremy Heiler <jeremyheiler@...> 2011/07/02

On Sat, Jul 2, 2011 at 1:39 PM, Joe Peric <peric.joe@gmail.com> wrote:

[#385102] symbol vs string for hash keys — Intransition <transfire@...>

What are the important factors to consider when deciding whether to

12 messages 2011/07/03

[#385160] Fatal Error with Ruby/GTK2, Homebrew, Green Shoes, and OSX Snow Leopard — Timothy Barnes <barnes7td@...>

I posted the below question to the Shoes mailing list and got back the

10 messages 2011/07/09

[#385194] RubyInstaller Extras — Jon <jon.forums@...>

Since 2009, we've been pleased to include Huw Collingbourne's The Book of Ruby with RubyInstaller. Well now, Huw has a newly updated version, and he's published it with No Starch Press.

11 messages 2011/07/12

[#385207] Creating a range of directories — Simon Harrison <simon@...>

I'm wanting to create a range of directories with a prefix, like the

13 messages 2011/07/13

[#385276] Is there a more efficient way to remove data from a string? — Eddie Catflap <catflaporama@...>

Hi

9 messages 2011/07/15

[#385297] Help needed for a new release of text-hyphen — Austin Ziegler <halostatue@...>

I've had folks asking me for a release of text-hyphen that works with

12 messages 2011/07/15

[#385326] Comparing hashes based on their keys — Stefano Mioli <stefano.mioli@...>

Hi list,

13 messages 2011/07/15

[#385342] moving a 1.8.6 install? — Roggie Boone <rogboone@...>

Is it possible to move the gems from a Ruby 1.8.6 install on an (old)

10 messages 2011/07/15

[#385410] Better name for #zip_with — Intransition <transfire@...>

Any ideas for a better name for:

13 messages 2011/07/18

[#385452] Ruby + USB + CCTV Camera Driver? — "Bob Carpenter." <rgc3679@...>

I want to write a Ruby driver that handles the feed from A CCTV camera.

23 messages 2011/07/18
[#385459] Re: Ruby + USB + CCTV Camera Driver? — William Rutiser <wruyahoo05@...> 2011/07/18

On 2011-07-18 1:00 PM, Bob Carpenter. wrote:

[#385461] Re: Ruby + USB + CCTV Camera Driver? — "Bob Carpenter." <rgc3679@...> 2011/07/18

William Rutiser wrote in post #1011466:

[#385462] Re: Ruby + USB + CCTV Camera Driver? — Hassan Schroeder <hassan.schroeder@...> 2011/07/18

On Mon, Jul 18, 2011 at 11:51 AM, Bob Carpenter. <rgc3679@yahoo.com> wrote:

[#385464] Re: Ruby + USB + CCTV Camera Driver? — "Bob Carpenter." <rgc3679@...> 2011/07/18

Hassan Schroeder wrote in post #1011472:

[#385471] Re: Ruby + USB + CCTV Camera Driver? — Hassan Schroeder <hassan.schroeder@...> 2011/07/18

On Mon, Jul 18, 2011 at 12:58 PM, Bob Carpenter. <rgc3679@yahoo.com> wrote:

[#385474] Re: Ruby + USB + CCTV Camera Driver? — Phillip Gawlowski <cmdjackryan@...> 2011/07/18

On Tue, Jul 19, 2011 at 12:18 AM, Hassan Schroeder

[#385477] Re: Ruby + USB + CCTV Camera Driver? — "Bob Carpenter." <rgc3679@...> 2011/07/19

Phillip Gawlowski wrote in post #1011503:

[#385509] A beginner question. — "Cajus A." <cajus_@...>

Hi i'm trying to write and example program where you speak to a

12 messages 2011/07/19

[#385542] Splitting strings — Ryan Mckenzie <ryan@...>

Hi all,

18 messages 2011/07/19

[#385565] error building ruby p290 — Dev Guy <devguy.ca@...>

I get the following build error on my Linux box while trying to build

12 messages 2011/07/20

[#385629] Are Job Ads OK? — Lee Hughes <lee@...>

Is it OK to post Ruby job ads on this forum?

12 messages 2011/07/21

[#385847] String manuplation example — kotin 76 <kotin76@...>

Hi,

15 messages 2011/07/27

[#385860] Counting lines of code in a ruby code file ignoring method code lines in the process — Harry Truax <htruax@...>

Hello,

8 messages 2011/07/27

[#385936] Help in Pointer — Tridib Bandopadhyay <tridib04@...>

Hello

18 messages 2011/07/29

[#386012] Ruby a "4th generation language"? Credible sources? — Albert Schlef <albertschlef@...>

I've noticed that Wikipedia says that "C#, Java" are 3'rd generation

13 messages 2011/07/31

[#386050] system stack error in ObjectSpace.each_object — luke gruber <luke.gru@...>

Hey guys, I'm just starting to mess around with metaprogramming in Ruby,

14 messages 2011/07/31
[#386052] Re: system stack error in ObjectSpace.each_object — Josh Cheek <josh.cheek@...> 2011/07/31

On Sun, Jul 31, 2011 at 4:29 PM, luke gruber <luke.gru@gmail.com> wrote:

[#386055] Re: system stack error in ObjectSpace.each_object — luke gruber <luke.gru@...> 2011/08/01

> Typically, SystemStackError is due to recursing for too long, perhaps I

[#386058] Re: system stack error in ObjectSpace.each_object — Robert Klemme <shortcutter@...> 2011/08/01

On Mon, Aug 1, 2011 at 3:56 AM, luke gruber <luke.gru@gmail.com> wrote:

Re: Comparing hashes based on their keys

From: Rob Biedenharn <Rob@...>
Date: 2011-07-15 17:18:41 UTC
List: ruby-talk #385329
On Jul 15, 2011, at 12:04 PM, Robert Klemme wrote:
> On Fri, Jul 15, 2011 at 5:38 PM, Stefano Mioli <stefano.mioli@gmail.com 
> > wrote:
>> Hi list,
>>    let's say we want to write a method that compares two hashes for  
>> equality
>> based on their keys.
>>
>> Each key is a symbol, and the corrisponding value can either be a  
>> string or
>> another hash with this same properties.
>>
>> Two hashes, let's call them h1 and h2, are to be considered equal if:
>>
>> * they have the same keys
>>
>> * h1[:x] and h2[:x] are both hashes, and they are equal according  
>> to the
>> very same rules you are reading ;)
>>
>> * they are both nil
>
> It seems your spec is not very precise because your bullet list mixes
> AND and OR.  I assume you meant
>
> Two objects o1 and o2 are considered equal for this relation if and  
> only if
>
> ( o1 is nil AND o2 is nil ) OR
> ( o1 is a Hash AND o2 is a Hash AND
>  o1.keys == o2.keys AND
>  for each key k ( o1[k] is not a Hash OR
>                          o2[k] is not a Hash OR
>                          o1[k] equals o2[k] according to this  
> relation ) )

I don't see why you couldn't just add your own method to Hash. (Tested  
on ruby-1.9.2)

class Hash
   def same_key_structure?(other)
     return false unless other.is_a?(Hash)
     return false unless self.keys == other.keys && self.keys.all?{|_| 
_.is_a?(Symbol)}
     self.keys.all? {|_|self[_].is_a?(Hash) ?  
self[_].same_key_structure?(other[_]) : !other[_].is_a?(Hash)}
   end
end

If you really cared about the nil case, you could do:

class NilClass
   def same_key_structure?(other)
     other.nil?
   end
end

But that's probably a stretch and only *you* can decided if it's right  
for the situation.

-Rob

P.S. I'm assuming that you don't consider supersets of keys to be  
"equal" and that you really do care that all the keys are symbols.

>
>> In short, we only care about the values when they are hashes.
>> If they are strings, we don't care whether they are equal or not.
>>
>> To make an example, when fed these two hashes the method should  
>> return true:
>>
>> h1 = {
>>  :one => "one",
>>  :two => "two",
>>  :three => {
>>    :alpha => "alpha",
>>    :bravo => "bravo",
>>    :charlie => "charlie"
>>  }
>> }
>>
>> h2 = {
>>  :one => "whatever",
>>  :two => "hey",
>>  :three => {
>>    :alpha => "zulu",
>>    :bravo => "oscar",
>>    :charlie => "brown"
>>  }
>> }
>>
>> When fed these other two arrays, the method should return false:
>>
>> h3 = h1
>>
>> h4 = {
>>  :one => "one",
>>  :two => "two",
>>  :three => {
>>    :alpha => "alpha",
>>    :bravo => "bravo",
>>  }
>> }
>>
>> The difference is that Ole :charlie is missing in h2.
>> The values don't change to make it clear that we don't care about  
>> them.
>>
>> I came up with the following implementation that seems to work (at  
>> least
>> according to the specs I wrote), but what I would like to ask is if  
>> it could
>> be any simpler/more idiomatic/more elegant.
>>
>> Corner cases that one might spot are also good to know about.
>>
>> def compare(hash1, hash2)
>
> I would rename arguments because these need not be Hashes.
>
>>  args = [hash1, hash2]
>>
>>  return true if args.all? {|h| h.nil?}
>>  return false if args.one? {|h| h.nil?}
>>
>>  hash1.each_key do |k|
>>    values = [hash1[k], hash2[k]]
>>
>>    if values.all? {|h| h.is_a?(Hash)}
>>      return compare(*values)
>>    else
>>      return false if values.one? {|value| value.nil? }
>>    end
>>  end
>>
>>  true
>> end
>
> Hm, I'd probably remove #all?, #any? and the like and code conditions
> directly - especially since you are always dealing with two items
> because it's likely faster.  Your code creates a lot of temporary
> Arrays along the way.
>
> Btw, I believe your implementation misses the case where hash2's key
> set is a superset of hash1's, i.e. contains additional keys.
>
> Kind regards
>
> robert
>
> -- 
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/
>

Rob Biedenharn		
Rob@AgileConsultingLLC.com	http://AgileConsultingLLC.com/
rab@GaslightSoftware.com		http://GaslightSoftware.com/


In This Thread