From: MonkeeSage Date: 2007-12-07T23:40:07+09:00 Subject: Re: function like "function_exits" On Dec 7, 8:16 am, Girard Fred wrote: > Jordan Callicoat wrote: > > On Dec 7, 7:31 am, Girard Fred wrote: > >> > Regards, > >> -------------------- > > >> Posted viahttp://www.ruby-forum.com/. > > But you don't really want that... ;) > > > You want respond_to? which Just Works everywhere. And really, you > > probably don't even want to be asking if a method is defined (depends > > on your code), but usually there are better ways of doing things (like > > unit testing). > > > HTH, > > Jordan > > No it's ok, i am happy with that, if i really want, i can merge these 2 > methods: > > def foo() > puts 'test' > end > > def function?(function_name) > return true if private_methods.include?(function_name) > return true if methods.include?(function_name) > return false > end > > puts function?('foo') => true > puts function?('notfoo') => false > puts function?('puts') => true > > It's nice to learn a new language like that, thank you so much (and > sorry for my poor english) > :) > > -- > Posted viahttp://www.ruby-forum.com/. Your english is fine. :) And I'm glad you like ruby! But really, you want to use respond_to? ...that's why it was added to the language: http://www.ruby-doc.org/core/classes/Object.html#M000333 In rbuy, everything is an object, and every callable object is a method. What it means when you say... foo() ...is really this... send(:foo) # actual ruby, try it! In other words, you don't "call" a method in ruby, you send an object a message that tells it to execute some code by that name. So, for example, when you say... [3,2,1].sort ...it really means... [3,2,1].send(:sort) # try it! So the way to check if an object can execute code with a certain name ("foo"), is to ask does it "respond_to?('foo')"...that means "can I send the object with a message 'foo' and it knows how to execute it?" If I don't make sense, just ignore me. ;) Regards, Jordan