# Hash#hash and detecting changes?

**URL:** <https://www.ruby-forum.com/t/hash-hash-and-detecting-changes/185478>\
**Category:** Ruby\
**Created:** [March 21, 2010, 2:43am UTC](https://www.ruby-forum.com/t/hash-hash-and-detecting-changes/185478 "2010-03-21T02:43:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Space\_Ship\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e274bd/32.png) [@Space\_Ship\_T](https://www.ruby-forum.com/u/Space_Ship_T)\
**Post date:** [March 21, 2010, 2:43am UTC](https://www.ruby-forum.com/t/hash-hash-and-detecting-changes/185478/1 "2010-03-21T02:43:31Z")

</div>

Hello,

I am writing a Rack session class. I want to detect changes to the  
session so that I don’t write the data to the session store if the  
underlying hash has not changed. Right now I’m doing the following but  
it doesn’t seem right:

```
  def call(env)
    load_session(env)
    status, headers, body = @app.call(env)
    commit_session(env, status, headers, body)
  end

```

load\_session loads env[“rack.session”] from the cookie, and also saves  
env[“rack.session”].hash into env[“myframework.session.hash”]

commit\_session checks env[“rack.session”].hash against the saved one  
(env[“myframework.session.hash”]) and if it is different, it writes the  
changes to disk.

The reason to do this is that writing data is expensive in this  
particular case, since it happens for many kinds of requests.

However my understanding is that while x.hash != y.hash implies that x  
and y are different, x.hash == y.hash does not imply that x and y are  
the same. Can someone comment on this?

Kind regards,  
Samuel

---

<div class="post-metadata">

**Author:** ![Space\_Ship\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e274bd/32.png) [@Space\_Ship\_T](https://www.ruby-forum.com/u/Space_Ship_T)\
**Post date:** [March 21, 2010, 7:17am UTC](https://www.ruby-forum.com/t/hash-hash-and-detecting-changes/185478/2 "2010-03-21T07:17:24Z")

</div>

On 3/20/2010 7:41 PM, Space Ship T. wrote:

> load\_session loads env[“rack.session”] from the cookie, and also saves env[“rack.session”].hash into env[“myframework.session.hash”]
> 
> commit\_session checks env[“rack.session”].hash against the saved one (env[“myframework.session.hash”]) and if it is different, it writes the changes to disk.
> 
> The reason to do this is that writing data is expensive in this particular case, since it happens for many kinds of requests.
> 
> However my understanding is that while x.hash != y.hash implies that x and y are different, x.hash == y.hash does not imply that x and y are the same. Can someone comment on this?
> 
> Kind regards,  
> Samuel

From [class Hash - RDoc Documentation](http://ruby-doc.org/core/classes/Hash.html#M002847)

hsh == other\_hash =\> true or false

Equality—Two hashes are equal if they each  
[http://ruby-doc.org/core/classes/Hash.html#M002861](http://ruby-doc.org/core/classes/Hash.html#M002861) contain the same  
number of keys [http://ruby-doc.org/core/classes/Hash.html#M002866](http://ruby-doc.org/core/classes/Hash.html#M002866) and  
if each [http://ruby-doc.org/core/classes/Hash.html#M002861](http://ruby-doc.org/core/classes/Hash.html#M002861) key-value  
pair is equal to (according to Object  
[http://ruby-doc.org/core/classes/Object.html](http://ruby-doc.org/core/classes/Object.html)#==) the corresponding  
elements in the other hash.

h1 = { “a” =\> 1, “c” =\> 2 }  
h2 = { 7 =\> 35, “c” =\> 2, “a” =\> 1 }  
h3 = { “a” =\> 1, “c” =\> 2, 7 =\> 35 }  
h4 = { “a” =\> 1, “d” =\> 2, “f” =\> 35 }  
h1 == h2 #=\> false  
h2 == h3 #=\> true  
h3 == h4 #=\> false

How much more equal do you need?

---

<div class="post-metadata">

**Author:** ![Space\_Ship\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e274bd/32.png) [@Space\_Ship\_T](https://www.ruby-forum.com/u/Space_Ship_T)\
**Post date:** [March 21, 2010, 7:22am UTC](https://www.ruby-forum.com/t/hash-hash-and-detecting-changes/185478/3 "2010-03-21T07:22:05Z")

</div>

On 3/21/2010 12:16 AM, Walton H. wrote:

> > ```
> > end
> > 
> > ```
> > 
> > Samuel  
> > pair is equal to (according to Object
> 
> How much more equal do you need?

Sorry for all the URLs, I just blindly pasted from my browser, not  
thinking about what would happen when Thunderbird changed it to plain  
text. It should read:

Equality—Two hashes are equal if they each  
[http://ruby-doc.org/core/classes/Hash.html#M002861](http://ruby-doc.org/core/classes/Hash.html#M002861) contain the same  
number of keys and if each key-value pair is equal to (according to  
Object [http://ruby-doc.org/core/classes/Object.html](http://ruby-doc.org/core/classes/Object.html)#==) the  
corresponding elements in the other hash.

h1 = { “a” =\> 1, “c” =\> 2 }  
h2 = { 7 =\> 35, “c” =\> 2, “a” =\> 1 }  
h3 = { “a” =\> 1, “c” =\> 2, 7 =\> 35 }  
h4 = { “a” =\> 1, “d” =\> 2, “f” =\> 35 }  
h1 == h2 #=\> false  
h2 == h3 #=\> true  
h3 == h4 #=\> false
