Hash#hash and detecting changes?

  • Thread starter Space Ship Traveller
  • Start date
S

Space Ship Traveller

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 =3D @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 !=3D y.hash implies that x =
and y are different, x.hash =3D=3D y.hash does not imply that x and y =
are the same. Can someone comment on this?

Kind regards,
Samuel=
 
W

Walton Hoops

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?
 
W

Walton Hoops

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
 
W

Walton Hoops

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
^^^ I give up, Thunderbird, you win.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top