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
On Nov 2, 2010, at 02:13 , Rahul K. wrote:
flag = (m == “write!” ? true : false)
CONGRATULATIONS! YOU JUST WON!!! Pet peeve numero uno:
a == b ? true : false
vs:
a == b
On Nov 2, 2010, at 02:13 , Rahul K. wrote:
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.
Ryan D. wrote in post #958662:
On Nov 2, 2010, at 02:13 , Rahul K. wrote:
flag = (m == “write!” ? true : false)
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.
On Tue, Nov 2, 2010 at 11:13 AM, Rahul K. [email protected]
wrote:
(btw, i’ve checked earlier threads:
Using the current method name within current method - Ruby - Ruby-Forum)
If I understood your purpose correctly, then this recent thread sheds
some light on aliases:
Is it possible to find out if an identifier is a method alias? - Ruby - Ruby-Forum
And this gist might be of interest as well:
alias_methods.rb · GitHub
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
On Nov 2, 2010, at 03:25 , Rahul K. wrote:
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.