From: Ross Bamford Date: 2006-03-29T23:38:44+09:00 Subject: Re: Why do some methods names which replace the content in placedoesn't have the "!" sign? On Wed, 2006-03-29 at 21:48 +0900, dblack@wobblini.net wrote: > Hi -- > > On Wed, 29 Mar 2006, Gregory Seidman wrote: > > > On Wed, Mar 29, 2006 at 12:56:25AM +0900, dblack@wobblini.net wrote: > > [...] > > } I'm talking about Ruby, not C. In Ruby, you can modify a receiver > > } without changing its instance variables -- in fact, you can modify a > > } receiver that doesn't *have* any instance variables. > > } > > } irb(main):001:0> s = "" > > } => "" > > } irb(main):002:0> s.replace("abc") > > } => "abc" > > } irb(main):003:0> s.instance_variables > > } => [] > > } > > } The term "instance variable" has a specific and unambiguous meaning in > > } Ruby. > > > > I repeat, it's a technicality. A string's internal state is held in > > variables tied to the particular instance. They are not visible to the Ruby > > runtime, however, so they are not listed in the array returned by the > > object's instance_variables method. Do you really believe that methods, > > which modify those variables, do not change the instance variables simply > > because the runtime is unaware of the variables? No, I don't think so. You > > are arguing a technicality. > > I'm not arguing at all. Someone asked a question about instance > variables (meaning Ruby instance variables, I assumed), and I answered > it. I assume, by default, that questions about Ruby are questions > about Ruby itself, rather than the C implementation. It's possible > that that's not what the OP meant, but in that case it's a good chance > for him to learn the customary use of the terminology. > > Obviously *something* changes when you change the contents of a string > or array. But calling it, without qualification, an "instance > variable" of the object is almost certainly a recipe for unclarity and > confusion down the road. If I can just chime in here, I agree with what I think is David's point that instance variables and 'state' are separate (though often overlapping) things. Should this be a bang method?: class PoolStr < String def suffixes @suffixes ||= find_suffixes end end Here, 'suffixes' uses an instance variable to cache the result, but I wouldn't say this modifies the object's state as far as the user of the API is concerned, since were it removed the class would work exactly the same, just more slowly. I think the distinction is between internal and external state (both of which may or may not be maintained in instance variables) - exactly what constitutes which of those is probably down to the guy who codes it up. -- Ross Bamford - rosco@roscopeco.REMOVE.co.uk