# Ruby is!

**URL:** <https://www.ruby-forum.com/t/ruby-is/156090>\
**Category:** Ruby\
**Created:** [December 20, 2008, 12:31pm UTC](https://www.ruby-forum.com/t/ruby-is/156090 "2008-12-20T12:31:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![selim007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@selim007](https://www.ruby-forum.com/u/selim007)\
**Post date:** [December 20, 2008, 12:31pm UTC](https://www.ruby-forum.com/t/ruby-is/156090/1 "2008-12-20T12:31:01Z")

</div>

Hello,  
I have a text-  
class TestStr \< String

> # attr\_accessor :dupstr def initialize ( str ) @dupstr = str super(str) end end ts=TestStr.new(“aaa”) puts ts.dupstr h=Hash.new() h[ts]=true puts h.keys.first.class puts h.keys.first puts h.keys.first.dupstr
> 
> run it:
> 
> $ ruby t.rb  
> aaa  
> TestStr  
> aaa  
> nil  
> $

> **[Live Sex & Live Porn Streaming on Free Live Sex Cams | livesex.com](http://www.scoreslive.com/en/)**
>
> Enjoy live porn and chat with girls and boys. Watch steaming hot shows in free live sex chat rooms. Hundreds of models online right now on LiveSex.com.

---

<div class="post-metadata">

**Author:** ![selim007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@selim007](https://www.ruby-forum.com/u/selim007)\
**Post date:** [December 24, 2008, 5:12pm UTC](https://www.ruby-forum.com/t/ruby-is/156090/2 "2008-12-24T17:12:57Z")

</div>

Selim Selim007 wrote:  
[…]

> > puts h.keys.first.dupstr

Selim’s question is an interesting one.

Could anybody explain why “h.keys.first.dupstr” doesn’t return “aaa” ?

---

<div class="post-metadata">

**Author:** ![selim007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@selim007](https://www.ruby-forum.com/u/selim007)\
**Post date:** [December 24, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/ruby-is/156090/3 "2008-12-24T18:45:00Z")

</div>

Albert S. wrote:

> Selim Selim007 wrote:  
> […]
> 
> > > puts h.keys.first.dupstr
> 
> Selim’s question is an interesting one.
> 
> Could anybody explain why “h.keys.first.dupstr” doesn’t return “aaa” ?

> cat ./test.rb  
> #!/usr/bin/env ruby -w  
> class TestStr \< String  
> attr\_accessor :dupstr  
> def initialize ( str )  
> @dupstr = str  
> super(str)  
> end  
> end  
> ts=TestStr.new(“aaa”)  
> puts ts.dupstr  
> h=Hash.new()  
> h[ts]=true  
> puts h.keys.first.class  
> puts h.keys.first  
> puts h.keys.first.dupstr  
> puts h.keys.first.frozen?

> ./test.rb  
> aaa  
> TestStr  
> aaa  
> nil  
> true

---

<div class="post-metadata">

**Author:** ![selim007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@selim007](https://www.ruby-forum.com/u/selim007)\
**Post date:** [December 25, 2008, 11:50am UTC](https://www.ruby-forum.com/t/ruby-is/156090/4 "2008-12-25T11:50:18Z")

</div>

> Could anybody explain why “h.keys.first.dupstr” doesn’t return “aaa” ?

When a Hash key is an unfrozen String, a special case kicks in: a copy  
is taken of the key.

irb(main):001:0\> key = “foo”  
=\> “foo”  
irb(main):002:0\> h = {key =\> 1}  
=\> {“foo”=\>1}  
irb(main):003:0\> h.keys  
=\> [“foo”]  
irb(main):004:0\> h.keys.first.object\_id  
=\> -605406218  
irb(main):005:0\> key.object\_id  
=\> -605406278

This avoids a nasty problem if the key is modified after insertion into  
the hash. In that case it could end up being on the wrong hash chain and  
therefore never be found when looked up.
