From: Rich Kilmer Date: 2001-10-29T23:29:46+09:00 Subject: [ruby-talk:23725] Re: ref. constructors and new (was: Re: class Foo does not call Class.new?) The parser would have to know that when you call: String.new("text") it is a special case because when you call: Foo.new("text") => Foo.new(String.new("text")) you don't want: String.new("text") => String.new(String.new("text")) I think ;-) -Rich > -----Original Message----- > From: dave@thomases.com [mailto:dave@thomases.com]On Behalf Of Dave > Thomas > Sent: Monday, October 29, 2001 9:22 AM > To: ruby-talk ML > Subject: [ruby-talk:23724] Re: ref. constructors and new (was: Re: class > Foo does not call Class.new?) > > > matz@ruby-lang.org (Yukihiro Matsumoto) writes: > > > |> They won't (at least String won't). It will cause infinite loop. > > |> > > |> "abc" -> String.new("abc") -> String.new(String.new("abc")) ... > > | > > |But String.new("abc") would become String.new(aNewStringObject), > > |wouldn't it? The literal only gets converted once. > > > > I'm not sure I get what you mean. Then how literal strings are > > created, without String.new? > > You showed a chain: > > "abc" -> String.new("abc") -> String.new(String.new("abc")) ... > > presumably to show that this would recurse indefinitely. But I don't > think this would be the case. > > When you evaluate a string literal, you call String.new, passing in > the contents of the literal. This will allocate space for the string > object, copy in the bytes from the string, set up the encoding, and > then call #initialize(newString) (that is: the parameter would be > equal to 'self' within initialize). I don't think there'd be a loop. > > In the case where you explicitly call > > String.new("abc") > > the literal string would be converted to a String object (as above) > before being passed to String.new as a parameter. String.new already > accepts String parameters, so again there'd be no infinite loop. > > I'm probably missing something obvious, though :) > > > Dave