From: Dave Thomas Date: 2001-10-29T23:21:32+09:00 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