get method name when aliasing

R

Rahul Kumar

I'd like to know the current method name or alias used, as follows:

def write
m = __method__
flag = (m == "write!" ? true : false)

some processing based on flag

end
alias :write! write

So if user calls write! flag is true, else false.

__method__ gives original name not alias used.

In irb, i tried: caller[0] =~ /`([^']*)'/ and $1
but this returns "irb_binding".

I might land up having quite a lot of such methods, so i'd rather not
make multiple methods with "!" calling the original ones.

(btw, i've checked earlier threads:
http://www.ruby-forum.com/topic/75258)

Regards, rkumar
 
R

Ryan Davis

I might land up having quite a lot of such methods, so i'd rather not
make multiple methods with "!" calling the original ones.

Why not? What's wrong with that? And if you're just at "might", why are =
you bothering?

I guess I should also point out that generally it is the non-bang =
methods that call the bang versions:

class String
def strip!
# mutate accordingly
end

def strip
self.dup.strip!
end
end

I don't see anything wrong with that implementation. It is easy to =
understand and doesn't carry the ridiculous overhead of overly clevar =
code.
 
R

Rahul Kumar

Ryan Davis wrote in post #958662:
CONGRA TULATIONS! YOU JUST WON!!! Pet peeve numero uno:

a == b ? true : false

vs:

a == b

My apologies. i typed that in a hurry just to give an idea.

My case is not the same as the ! in ruby methods. Its more like in Vim,
where "write!" will not prompt if the file exists, whereas "write" will.

Regarding "might", I'd rather have this thought out at the start, rather
than have to rewrite a whole lot of code later.
 
A

Ammar Ali

I'd like to know the current method name or alias used, as follows:

def write
=C2=A0 m =3D __method__
=C2=A0 flag =3D (m =3D=3D "write!" ? true : false)

=C2=A0 some processing based on flag

end
alias :write! write

So if user calls write! flag is true, else false.

__method__ gives original name not alias used.

In irb, i tried: caller[0] =3D~ /`([^']*)'/ and $1
but this returns "irb_binding".

I might land up having quite a lot of such methods, so i'd rather not
make multiple methods with "!" calling the original ones.

(btw, i've checked earlier threads:
http://www.ruby-forum.com/topic/75258)

If I understood your purpose correctly, then this recent thread sheds
some light on aliases:

http://www.ruby-forum.com/topic/232274

And this gist might be of interest as well:

http://gist.github.com/631102

However, IMHO, if one needs such functionality, then it is probably a
sign that the design needs review. I put this gist together to
discover aliases and satisfy my curiosity, only.

Regards,
Ammar
 
R

Ryan Davis

Regarding "might", I'd rather have this thought out at the start, = rather
than have to rewrite a whole lot of code later.

This is a classic mistake and leads to unnecessary complications and =
hard-to-maintain code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top