R_Kumar
(R. Kumar)
2 November 2010 09:13
1
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
···
--
Posted via http://www.ruby-forum.com/ .
CONGRATULATIONS! YOU JUST WON!!! Pet peeve numero uno:
a == b ? true : false
vs:
a == b
···
On Nov 2, 2010, at 02:13 , Rahul Kumar wrote:
flag = (m == "write!" ? true : false)
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.
···
On Nov 2, 2010, at 02:13 , Rahul Kumar 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.
Ammar_Ali
(Ammar Ali)
2 November 2010 10:56
4
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:
631102’s gists · 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 Tue, Nov 2, 2010 at 11:13 AM, Rahul Kumar <sentinel1879@gmail.com> wrote:
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\ )
R_Kumar
(R. Kumar)
2 November 2010 10:25
5
Ryan Davis wrote in post #958662:
···
On Nov 2, 2010, at 02:13 , Rahul Kumar 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.
--
Posted via http://www.ruby-forum.com/\ .
This is a classic mistake and leads to unnecessary complications and hard-to-maintain code.
···
On Nov 2, 2010, at 03:25 , Rahul Kumar wrote:
Regarding "might", I'd rather have this thought out at the start, rather
than have to rewrite a whole lot of code later.