From: "Hal E. Fulton" Date: 2002-06-10T12:11:16+09:00 Subject: Re: Getting method! for free from method ----- Original Message ----- From: "Philip Mak" To: "ruby-talk ML" Sent: Sunday, June 09, 2002 9:53 PM Subject: Re: Getting method! for free from method > On Mon, Jun 10, 2002 at 11:21:23AM +0900, james@rubyxml.com wrote: > > > Is there a shortcut for telling ruby to define method! for me? > > > > method! would suggest that there were some permanent side effects to the > > receiver, and whether or not that's true depends on the specific code. > > > > If you have > > > > def right_now > > Time.new.to_s > > end > > > > then what would right_now! do? > > With the way I'm thinking of it, myObj.right_now! would be equivalent > to: > > myObj = myObj.right_now I see a problem. In this case, with the given definition of right_now, these two can never be equivalent. (Correct me if I'm wrong.) The reason is that applying a method to an object (or sending a message to a receiver -- choose your parlance) never changes the identity of that object. That is, the type or class never changes. But when you say x = x.y, you are assigning a value to a variable, potentially replacing the object reference with a reference to a new, different object (as in this case). Remember, methods operate on objects, not on variables (hence the confusion that many newbies have with "freeze"). No way around this that I can see. Nor would I really *want* a way around it. Some people (Smalltalkers?) want to be able to change the class of an object dynamically. Maybe they have a point. As for me, it makes me nervous. Someone once suggested making variables into first-class objects... I think I see some merit in this, but it might raise more issues than it would solve. Hal Fulton to