On 3/21/2010 12:16 AM, Walton Hoops wrote:
> On 3/20/2010 7:41 PM, Space Ship Traveller wrote:
>   
>> 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
>>   
>>     
> From 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> contain the same
> number of keys <http://ruby-doc.org/core/classes/Hash.html#M002866> and
> if each <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>#==) 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?
>
>   
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> 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>#==) 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