# Can an object obj-arbitrary respond to a method with arbitary name?

**URL:** <https://www.ruby-forum.com/t/can-an-object-obj-arbitrary-respond-to-a-method-with-arbitary-name/132235>\
**Category:** Rails\
**Created:** [March 18, 2008, 6:48am UTC](https://www.ruby-forum.com/t/can-an-object-obj-arbitrary-respond-to-a-method-with-arbitary-name/132235 "2008-03-18T06:48:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [March 18, 2008, 6:48am UTC](https://www.ruby-forum.com/t/can-an-object-obj-arbitrary-respond-to-a-method-with-arbitary-name/132235/1 "2008-03-18T06:48:25Z")

</div>

For example:  
I create an object c.  
Now, if I call c.xxx, ruby just raise an exception ‘method missing’  
But, I want it return nil, for each method which is not pre-defined.  
Is there a way I can gain this ?  
Thanks!

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [March 18, 2008, 7:05am UTC](https://www.ruby-forum.com/t/can-an-object-obj-arbitrary-respond-to-a-method-with-arbitary-name/132235/2 "2008-03-18T07:05:24Z")

</div>

If you are doing something like this s = c.xxx then u can always give  
recue nil which would then set the value of s as nil.

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [March 18, 2008, 7:32am UTC](https://www.ruby-forum.com/t/can-an-object-obj-arbitrary-respond-to-a-method-with-arbitary-name/132235/3 "2008-03-18T07:32:58Z")

</div>

Thank you!~  
I imaged there may be some trigger, open it, then object of some class  
will response to every method. close it, then exception raises.  
I wanna the classes predefined by ruby/rails can have this  
functionality.  
Can anyone tell me how to diy it ?  
For example, I want a model class has this kind of trigger.

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [March 18, 2008, 8:49am UTC](https://www.ruby-forum.com/t/can-an-object-obj-arbitrary-respond-to-a-method-with-arbitary-name/132235/4 "2008-03-18T08:49:28Z")

</div>

Then I think yo need to write your custom method\_missing in you model  
class. This is how rails actually creates its dynamic finders by  
invoking method\_missing in base.rb. Or you can actually modify the  
method\_missing in base.rb as per your need.

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [March 18, 2008, 9:58am UTC](https://www.ruby-forum.com/t/can-an-object-obj-arbitrary-respond-to-a-method-with-arbitary-name/132235/5 "2008-03-18T09:58:12Z")

</div>

Thank very much!  
That’s just I want.  
I lack of knowledge about how rails works, I think.  
Thanks a lot!!
