Getting method! for free from method


(Philip Mak) #1

Let’s say I have:

def method
# do something
end

Is there a shortcut for telling ruby to define method! for me?


(Ned Konz) #2

No. How would it know what you wanted method!() to do?

Unless the DWIM module is installed, of course…

···

On Sunday 09 June 2002 07:06 pm, Philip Mak wrote:

Let’s say I have:

def method

do something

end

Is there a shortcut for telling ruby to define method! for me?


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE


(James) #3

Let’s say I have:

def method

do something

end

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?

James


(Kent Dahl) #4

Philip Mak wrote:

Let’s say I have:

def method
# do something
end

Is there a shortcut for telling ruby to define method! for me?

As others have pointed out, nope.

Just like to share that I tend to do the opposite sometimes, and that
isn’t so difficult:

def method!
# mutate the object
end
def method
result = self.dup
result.method!
end

Well, this works if the intent and behaviour of dup isn’t at odds. And
you could probably make an evil-eval module function for a shortcut that
makes the non-mutator version of the method for you.

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 4. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)


(Philip Mak) #5

I was thinking of something like this:

#define o.method!(x) o = o.method(x)

meaning that for example this code:

x = "hello"
x.upcase!

would be interpreted like this:

x = "hello"
x = x.upcase

···

On Mon, Jun 10, 2002 at 11:19:16AM +0900, Ned Konz wrote:

def method
# do something
end

Is there a shortcut for telling ruby to define method! for me?

No. How would it know what you wanted method!() to do?


(Philip Mak) #6

With the way I’m thinking of it, myObj.right_now! would be equivalent
to:

myObj = myObj.right_now

···

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?


(HAL 9000) #7

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

···

----- Original Message -----
From: “Philip Mak” pmak@animeglobe.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
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


(James) #8

meaning that for example this code:

x = "hello"
x.upcase!

would be interpreted like this:

x = "hello"
x = x.upcase

If you can define an expression for deriving some_method! if you already
have some_method, then you may be able to use method_missing.

You could see if method_missing is given a method name that ends with “!”;
if so, handle the request by deriving the name of the corresponding method
and dynamically invoke it (or raise an exception).

James

···

(Ned Konz) #9

Most Smalltalks have #become:, which works like:

objOne become: objTwo

swaps the identity of objOne and objTwo

There is also often a one-way #become:.

It’s not used much, but is really useful when it is needed… for
instance, imagine replacing a proxy object with a real object in an
object database or distributed object system.

···

On Sunday 09 June 2002 08:11 pm, Hal E. Fulton wrote:

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.


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE