On 12/13/06, Jan Svitok <jan.svitok gmail.com> wrote:
> On 12/13/06, murphy <murphy rubychan.de> wrote:
> > > I would expect the second puts to return 5,
not nil. In fact, I'd expect s.to_a[0] to return the same
object as a.
> > no problem with the subclass. the problem is here:
> >
> > require 'set'
> > a = 'test' # => "test"
> > a.object_id # => 931080
> > Set[a].to_a.first.object_id # => 931110
> >
> > why does Set duplicate its elements? Hash and
Array don't do this.
> > [murphy]
>
> So the problem is here:
>
> ht
tp://ruby-doc.org/core/classes/Hash.html#M002883
> --------
> hsh[key] = value => value
> hsh.store(key, value) => value
>
> Element Assignment—Associates the value given by value
with the key
> given by key. key should not have its value changed
while it is in use
> as a key (a String0 passed as a key will be duplicated
and frozen).
> --------
>
> Set is implemented over Hash, using its .keys as the
storage. (see
> lib/1.8/set.rb)
> Hash duplicates its keys to prevent the change.
>
> if we add .freeze then it would return the same object:
>
> require 'set'
> a = 'test'.freeze # => "test"
> a.object_id # => 931080
> Set[a].to_a.first.object_id # => 931080 or whatever
it is
The real problem is in string.c:str_new4() that copies only
string
content and doesn't copy any other data.
Solution would be either to fix that or to not derive from
String -
working approach would be to have a String member and
forward the
calls to it (using Forwardable or something similar).
|