# Override method\_missing()

**URL:** <https://www.ruby-forum.com/t/override-method-missing/212971>\
**Category:** IronRuby\
**Created:** [October 19, 2011, 5:24pm UTC](https://www.ruby-forum.com/t/override-method-missing/212971 "2011-10-19T17:24:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [October 19, 2011, 5:24pm UTC](https://www.ruby-forum.com/t/override-method-missing/212971/1 "2011-10-19T17:24:29Z")

</div>

Hi,  
Is there a way to override the method\_missing method and keep the  
SetVariable lookup behavior from ScriptScope?  
For example,

in C#  
aScriptScope.SetVariable(“a”, “some\_value”)

in Ruby  
p a  
=\> “some\_value”

But if I do,

class \<\< self  
def method\_missing(sym, \*args, &block)  
case (sym)  
when :some\_case  
do\_something  
else  
super #try to fallback to the original  
end  
end  
end

or

class \<\< self  
alias :old\_method\_missing :method\_missing  
def method\_missing(sym, \*args, &block)  
case (sym)  
when :some\_case  
do\_something  
else  
old\_method\_missing(sym, \*args, &block) #try to fallback to the  
original  
end  
end  
end

p a  
=\> NameError: undefined local variable or method ‘a’ for main:Object

---

<div class="post-metadata">

**Author:** ![lewis\_l](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@lewis\_l](https://www.ruby-forum.com/u/lewis_l)\
**Post date:** [October 19, 2011, 6:13pm UTC](https://www.ruby-forum.com/t/override-method-missing/212971/2 "2011-10-19T18:13:20Z")

</div>

Might be a bug.

---

<div class="post-metadata">

**Author:** ![lewis\_l](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@lewis\_l](https://www.ruby-forum.com/u/lewis_l)\
**Post date:** [October 19, 2011, 7:00pm UTC](https://www.ruby-forum.com/t/override-method-missing/212971/3 "2011-10-19T19:00:09Z")

</div>

Thanks a lot. At least I won’t be killing myself trying to figure out  
what I missed. FYI, I tested on version 1.1 and 1.1.3 and they seem to  
have the same issue.  
Since we are on the subject, I wonder if you can spare some of your time  
help me understand this:

As far as I understand, the global environment is actually a singleton  
‘main’ Object. The function defined in the global scope should be  
translated to private method of Object class which is what it shows in  
iirb.bat. Not sure why ir.exe as well as the code in the ScriptEngine  
does not behave that way.

c:\Program Files\IronRuby 1.1\bin\>ir  
IronRuby 1.1.3.0 on .NET 4.0.30319.225  
Copyright (c) Microsoft Corporation. All rights reserved.

> > > def global\_hi  
> > > … puts ‘hi’  
> > > … end  
> > > =\> nil  
> > > class A  
> > > … def hi  
> > > … global\_hi  
> > > … end  
> > > … end  
> > > =\> nil  
> > > A.new.hi  
> > > (ir):3:in `hi': undefined method `global\_hi’ for #\<A:0x0000056\>  
> > > (NoMethodError)  
> > > from (ir):1

> > > self.method(:global\_hi)  
> > > =\> #\<Method: Object(#\<Class:#Object:0x0000058\>)#global\_hi\>  
> > > exit

* * *

c:\Program Files\IronRuby 1.1\bin\>iirb  
irb(main):001:0\> def global\_hi  
irb(main):002:1\> puts ‘hi’  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> class A  
irb(main):005:1\> def hi  
irb(main):006:2\> global\_hi  
irb(main):007:2\> end  
irb(main):008:1\> end  
=\> nil  
irb(main):009:0\> A.new.hi  
hi  
=\> nil  
irb(main):010:0\> self.method(:global\_hi)  
=\> #\<Method: Object#global\_hi\>  
irb(main):011:0\>
