Hi,
I have one basic question to ask regarding Hash key. Follow the below :
"foo".object_id # => 72994000
"foo".object_id # => 72993390
{"foo" =>2,"foo" =>3} # => {"foo"=>3}
I do know that Hash don''t allow duplicate keys. But I would like to by
which method hash check if any duplicate key present into it or not? As
I can see "foo" have different `object_id`,which is expected. But in
case of Hash key how this two different objects "foo" is treated as
same
object?
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/0aaf2c2143754731d6dd53614fa5dc84%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.
h = Hash.new
h.has_key?(''foo'')
If you try to create duplicate keys, the existing value is replaced by the new
one.
h = {"foo" =>2,"foo" =>3}
h # => {"foo"=>3}
h[''foo''] = ''bar''
h # => {"foo"=>''bar''}
--
Dheeraj Kumar
On Tuesday 10 September 2013 at 11:02 AM, Love U Ruby wrote:
> Hi,
>
> I have one basic question to ask regarding Hash key. Follow the below :
>
> "foo".object_id # => 72994000
> "foo".object_id # => 72993390
> {"foo" =>2,"foo" =>3} # =>
{"foo"=>3}
>
> I do know that Hash don''t allow duplicate keys. But I would like
to by
> which method hash check if any duplicate key present into it or not? As
> I can see "foo" have different `object_id`,which is expected. But
in
> case of Hash key how this two different objects "foo" is treated
as same
> object?
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
(mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org).
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
(mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org).
> To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/0aaf2c2143754731d6dd53614fa5dc84%40ruby-forum.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/67CA64BF9D3049DAB5CDC40BF4530C8D%40gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
On Sep 10, 2013, at 12:32 AM, Love U Ruby <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > > I have one basic question to ask regarding Hash key. Follow the below : > > "foo".object_id # => 72994000 > "foo".object_id # => 72993390 > {"foo" =>2,"foo" =>3} # => {"foo"=>3} > > I do know that Hash don''t allow duplicate keys. But I would like to by > which method hash check if any duplicate key present into it or not? As > I can see "foo" have different `object_id`,which is expected. But in > case of Hash key how this two different objects "foo" is treated as same > object?The hash''s keys'' object id''s are not what is compared. If you take both of those strings, and did this: "foo" == "foo" You''d get true, regardless of the fact they have different object ids. You are making this way way way more complicated than it is. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/B533A677-B93F-4E37-8C23-302611BA741A%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On Tuesday, September 10, 2013 8:27:13 AM UTC+1, tamouse wrote:> > > I do know that Hash don''t allow duplicate keys. But I would like to by > > which method hash check if any duplicate key present into it or not? As > > I can see "foo" have different `object_id`,which is expected. But in > > case of Hash key how this two different objects "foo" is treated as same > > object? > > The hash''s keys'' object id''s are not what is compared. If you take both of > those strings, and did this: > > "foo" == "foo" > > You''d get true, regardless of the fact they have different object ids. > > You are making this way way way more complicated than it is. > >By default yes, eql? is used for hash keys (and eql? in turn generally calls ==). You can ask for a hash to use object identity though: h = {}.compare_by_identity => {} h[''a''] = 1 # => 1 h[''a''] = 2 # => 2 h # => {"a"=>1, "a"=>2} Although it''s not something I''ve ever needed or seen in the wild. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e48dbc9e-6647-4820-a505-6141721e9a73%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.