# When I say method\_defined? I mean it!

**URL:** <https://www.ruby-forum.com/t/when-i-say-method-defined-i-mean-it/99421>\
**Category:** Ruby\
**Created:** [July 22, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/when-i-say-method-defined-i-mean-it/99421 "2006-07-22T16:27:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 22, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/when-i-say-method-defined-i-mean-it/99421/1 "2006-07-22T16:27:28Z")

</div>

I’ve been bitten by this way too many times now. And I’m just plain  
tired of it.

Today’s case:

def wrap\_method( sym, &blk )  
raise ArgumentError, “method does not exist – #{sym}” unless  
method\_defined?( sym )  
old = instance\_method(sym)  
define\_method(sym) { |\*args| blk.call(old.bind(self), \*args) }  
end

Seems all well and good until now when it comes out that  
#method\_defined?, like #instance\_methods, does something only a Ruby  
hacker without a life would expect. The actual _monster_ code needed  
is:

def wrap\_method( sym, &blk )  
unless public\_method\_defined?( sym ) || private\_method\_defined?(  
sym ) || protected\_method\_defined?( sym )  
raise ArgumentError, “method does not exist – #{sym}”  
end  
old = instance\_method(sym)  
define\_method(sym) { |\*args| blk.call(old.bind(self), \*args) }  
end

Now, it just doesn’t stand to any good reason. When we wan’t to  
actually grab a method, we don’t use #public\_instance\_method, do we?  
That would be a nightmare! And so it is with the above case and many  
others I’ve delt with. Every time I have to go back and fix this “bug”  
recalling the terribly inconvenient and anti-literal definition of  
these methods-methods.

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 22, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/when-i-say-method-defined-i-mean-it/99421/2 "2006-07-22T18:46:06Z")

</div>

> def wrap\_method( sym, &blk )  
> raise ArgumentError, “method does not exist – #{sym}” unless method\_defined?( sym )  
> old = instance\_method(sym)  
> define\_method(sym) { |\*args| blk.call(old.bind(self), \*args) }  
> end

Replace “unless method\_defined?( sym )” with “unless  
(method(sym) rescue nil)”.

But you don’t need this check. instance\_method(sym), on the  
next line, already performs this check for you.

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 23, 2006, 2:57am UTC](https://www.ruby-forum.com/t/when-i-say-method-defined-i-mean-it/99421/3 "2006-07-23T02:57:22Z")

</div>

I think Mr. Veenstra’s approach is fine–either allow the exception  
from instance\_method() to percolate up, or rescue it and throw your  
own. As far as a comprehensive check goes, wouldn’t ‘unless  
method\_defined? || private\_method\_defined?’ suffice? Or you could, I  
guess, use

```
unless Object.new.extend(self).respond_to?(sym, true)

```

which seems more amusing.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 22, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/when-i-say-method-defined-i-mean-it/99421/4 "2006-07-22T19:06:08Z")

</div>

Erik V. wrote:

> > def wrap\_method( sym, &blk )  
> > raise ArgumentError, “method does not exist – #{sym}” unless method\_defined?( sym )  
> > old = instance\_method(sym)  
> > define\_method(sym) { |\*args| blk.call(old.bind(self), \*args) }  
> > end
> 
> Replace “unless method\_defined?( sym )” with “unless  
> (method(sym) rescue nil)”.

Tha won’t work b/c it would be looking at the module or classes  
methods, not the instance methods.

> But you don’t need this check. instance\_method(sym), on the  
> next line, already performs this check for you.

Oh right, thanks, I’ll change.

Nonetheless if I wanted to do something else with the check, the point  
of my post remains. It silly to have to check

public\_methods || private\_methods || protected\_methods

When #instance\_methods should just correspond to the behavior to  
#instance\_method. I mean, isn;t reasonable to think those two methods  
would have the same lookup behavior?

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 23, 2006, 4:06am UTC](https://www.ruby-forum.com/t/when-i-say-method-defined-i-mean-it/99421/5 "2006-07-23T04:06:01Z")

</div>

Dumaiu wrote:

> I think Mr. Veenstra’s approach is fine–either allow the exception  
> from instance\_method() to percolate up, or rescue it and throw your  
> own. As far as a comprehensive check goes, wouldn’t ‘unless  
> method\_defined? || private\_method\_defined?’ suffice? Or you could, I  
> guess, use
> 
> unless Object.new.extend(self).respond\_to?(sym, true)
> 
> which seems more amusing.

I think my point is being missed. Forget my last example, it has  
nothing to do with it --it was just a special case --one in which I  
didn’t end up needing to check if the method existed b/c it was going  
to do what I was telling it do anyway. I could just as easily taken a  
different route.

My point is about the semantic meaning of important methods about  
methods. Think about it like Dr. Suess. Let’s say I have these methods:

big\_fish #=\> [“tuna”, “saw”]  
small\_fish #=\> [“trout”, “croaker”]  
tiny\_fish #=\> [“beta”]

So what woud expect from just:

fish #=\> ?

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 23, 2006, 9:21am UTC](https://www.ruby-forum.com/t/when-i-say-method-defined-i-mean-it/99421/6 "2006-07-23T09:21:28Z")

</div>

Tempting as it is to respond to Dr. Seuss, I think it makes more  
sense to ask you to state your point as succinctly as possible so as to  
prevent further misunderstanding. I’m guessing you dislike the fact  
that method\_defined?() doesn’t consider private methods–?

-J

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 23, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/when-i-say-method-defined-i-mean-it/99421/7 "2006-07-23T14:06:59Z")

</div>

Dumaiu wrote:

> Tempting as it is to respond to Dr. Seuss, I think it makes more  
> sense to ask you to state your point as succinctly as possible so as to  
> prevent further misunderstanding. I’m guessing you dislike the fact  
> that method\_defined?() doesn’t consider private methods–?

🙂 Yes --and protected. method\_defined? is one of the culprits, and so  
are #methods and #instance\_methods. Here’s my Dr. Seuss analogy  
de-anaologized:

public\_instance\_methods #=\> [“tuna”, “saw”]  
private\_instance\_methods #=\> [“trout”, “croaker”]  
protected\_instance\_methods #=\> [“beta”]

So what woud expect from just:

instance\_methods #=\> ?

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 24, 2006, 4:26am UTC](https://www.ruby-forum.com/t/when-i-say-method-defined-i-mean-it/99421/8 "2006-07-24T04:26:17Z")

</div>

A quick test I did in irb suggests that methods(),  
instance\_methods() and method\_defined?() all register protected  
members, meaning that only the, uh, privates are omitted. I concede  
that it can be confusing, but there’s no point submitting a RCR without  
an understanding of the current rationale. I suppose the idea is that  
because private methods are implementational, you shouldn’t have to see  
them without specifically asking. Of course, prohibiting access would  
be out of character for Ruby; hence the optional respond\_to?()  
parameter.  
I might like having an optional switch for instance\_methods(), too,  
but in all I think the setup is satisfactorily logical and consistent.  
My thinking on the matter is a bit clearer now. How about you?

```
-J
```
