List Info

Thread: Sets and String subclasses




Sets and String subclasses
user name
2006-12-13 17:26:05
Bugs item #7253, was opened at 2006-12-13 12:26
You can respond by visiting: 
http://rubyforge.org/tracke
r/?func=detail&atid=1698&aid=7253&group_id=426

Category: None
Group: None
Status: Open
Resolution: None
Priority: 3
Submitted By: Tim Smith (tsmith)
Assigned to: Nobody (None)
Summary: Sets and String subclasses

Initial Comment:
In Ruby 1.8.4, 1.8.5, and as of 1.9.0 (2006-04-15):

class A < String
  attr_accessor 
end

a = A.new 'hello'
a.x = 5
s = Set[a]
puts a.x # => 5
puts s.to_a[0].x # => nil

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.

------------------------------------------------------------
----------

You can respond by visiting: 
http://rubyforge.org/tracke
r/?func=detail&atid=1698&aid=7253&group_id=426

Sets and String subclasses
user name
2006-12-13 17:39:35
> 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]

Sets and String subclasses
user name
2006-12-13 20:54:19
On 12/13/06, murphy <murphyrubychan.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

Sets and String subclasses
user name
2006-12-13 21:11:50
On 12/13/06, Jan Svitok <jan.svitokgmail.com> wrote:
> On 12/13/06, murphy <murphyrubychan.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).

[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )