From: Magnus Holm Date: 2011-06-17T01:00:40+09:00 Subject: [ruby-core:37171] Re: [Ruby 1.9 - Bug #4893][Open] Literal Instantiation breaks Object Model --0016367f959ac03cfc04a5d65e46 Content-Type: text/plain; charset=UTF-8 How exactly do you expect this to work? If the string literal is supposed to be created with String.new(), what do we pass to String.new()? If we pass a simple string (that is, a string which hasn't gone through String.new()), these two behave different: "Hello World" # => Calls String.new() with a simple string and returns a full string String.new("Hello World") # => Calls String.new() with string literal (a full string) and returns a full string Beside, I don't see how this breaks the object model. The object model doesn't state that all created strings must be created through String.new. In fact, it's possible to side-step the initialization in pure Ruby too: String.allocate.replace("Hello"). // Magnus Holm On Thu, Jun 16, 2011 at 17:46, Lazaridis Ilias wrote: > > Issue #4893 has been reported by Lazaridis Ilias. > > ---------------------------------------- > Bug #4893: Literal Instantiation breaks Object Model > http://redmine.ruby-lang.org/issues/4893 > > Author: Lazaridis Ilias > Status: Open > Priority: Normal > Assignee: > Category: > Target version: > ruby -v: 1.9.2 > > > > #String2.rb > class String > def initialize(val) > self.replace(val) > puts object_id > end > def my_method_test > 'has method ' > end > end > > # command line > $ irb > irb(main):001:0> original = String.new("original") > => "original" > irb(main):002:0> load "String2.rb" > => true > irb(main):003:0> altered = String.new("altered") > 21878604 > => "altered" > irb(main):004:0> altered.my_method_test > => "has method " > irb(main):005:0> literal = "literal" > => "literal" > irb(main):006:0> literal.my_method_test > => "has method " > irb(main):007:0> > > - > > The initialize method is an integral part of the class String. > From the moment that "String2.rb" is loaded, the initialize method of > class String has been validly redefined. > > (The behaviour of the String class within the "irb session" is > altered) > > The altered initialize method is now an integral part of the class > String. > > The altered String object behaves as expected (responds to > "my_method_test, initialized via redefined initialize method). > > The String(Literal) object responds to "my_method_test", but it is was > not initialized with the redefined initialize method. > > - > > The "Literal Instantiation" calls the original (core-C-level) String > initialize method instead of the redefined one (user-language-level). > This *breaks* the object model. > > > > > > -- > http://redmine.ruby-lang.org > > --0016367f959ac03cfc04a5d65e46 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable How exactly do you expect this to work? If the string=C2=A0literal=C2=A0is = supposed to be created with String.new(), what do we pass to String.new()? = If we pass a simple string (that is, a string which hasn't gone through= String.new()), these two behave different:

=C2=A0 "Hello World" =C2=A0# =3D> Calls String.= new() with a simple string and returns a full string
=C2=A0 Strin= g.new("Hello World") # =3D> Calls String.new() with string lit= eral (a full string) and returns a full string

Beside, I don't see how this breaks the object mode= l. The object model doesn't state that all created strings must be crea= ted through String.new. In fact, it's possible to side-step the initial= ization in pure Ruby too:=C2=A0String.allocate.replace("Hello").<= /div>

// Magnus Holm


On Thu, Jun 16, 2011 at 17:46, Lazaridis= Ilias <ilias@l= azaridis.com> wrote:

Issue #4893 has been reported by Lazaridis Ilias.

----------------------------------------
Bug #4893: Literal Instantiation breaks Object Model
http= ://redmine.ruby-lang.org/issues/4893

Author: Lazaridis Ilias
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: 1.9.2



#String2.rb
class String
=C2=A0def initialize(val)
=C2=A0 =C2=A0self.replace(val)
=C2=A0 =C2=A0puts object_id
=C2=A0end
=C2=A0def my_method_test
=C2=A0 =C2=A0'has method <my_method_test>'
=C2=A0end
end

# command line
$ irb
irb(main):001:0> original =3D String.new("original")
=3D> "original"
irb(main):002:0> load "String2.rb"
=3D> true
irb(main):003:0> altered =3D String.new("altered")
21878604
=3D> "altered"
irb(main):004:0> altered.my_method_test
=3D> "has method <my_method_test>"
irb(main):005:0> literal =3D "literal"
=3D> "literal"
irb(main):006:0> literal.my_method_test
=3D> "has method <my_method_test>"
irb(main):007:0>

-

The initialize method is an integral part of the class String.
From the moment that "String2.rb" is loaded, the initialize metho= d of
class String has been validly redefined.

(The behaviour of the String class within the "irb session" is altered)

The altered initialize method is now an integral part of the class
String.

The altered String object behaves as expected (responds to
"my_method_test, initialized via redefined initialize method).

The String(Literal) object responds to "my_method_test", but it i= s was
not initialized with the redefined initialize method.

-

The "Literal Instantiation" calls the original (core-C-level) Str= ing
initialize method instead of the redefined one (user-language-level).
This *breaks* the object model.





--
http://redmine.r= uby-lang.org


--0016367f959ac03cfc04a5d65e46--