# Access Top Level methods

**URL:** <https://www.ruby-forum.com/t/access-top-level-methods/208944>\
**Category:** IronRuby\
**Created:** [June 30, 2011, 11:18pm UTC](https://www.ruby-forum.com/t/access-top-level-methods/208944 "2011-06-30T23:18:37Z")\
**Posts on this page:** 4\
**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:** [June 30, 2011, 11:18pm UTC](https://www.ruby-forum.com/t/access-top-level-methods/208944/1 "2011-06-30T23:18:37Z")

</div>

Hi,

I am trying to access top level method from the class definition in  
IronRuby scripts. Example:

def global\_hi  
end

class A  
def hi  
global\_hi  
end  
end

A.new.hi

The script executed in ScriptEngine throws a NoSuchMethodError.  
I tried to ran the same code in iirb.exe It seems to behave the way I  
expected. But when I tried it in ir.exe, it throws the exception as  
well.  
I investigate a little more and found out

irb(main):021:0\> self.method(:global\_hi)  
=\> #\<Method: Object#global\_hi\>  
and  
in ir.exe as well as script engine

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

Is there anyway to make the script behave like the one in iirb.exe?

Thanks,  
Lewis

---

<div class="post-metadata">

**Author:** ![Lewis\_Lin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ea666f/32.png) [@Lewis\_Lin](https://www.ruby-forum.com/u/Lewis_Lin)\
**Post date:** [July 20, 2011, 3:18pm UTC](https://www.ruby-forum.com/t/access-top-level-methods/208944/2 "2011-07-20T15:18:17Z")

</div>

I cannot reproduce this issue with IronRuby 1.1.3.0 on .NET  
4.0.30319.235  
It works as expected: ir.exe result is same as irb result.

Regards

From: Lewis Lin  
Sent: Thursday, June 30, 2011 9:58 PM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: [Ironruby-core] Access Top Level methods

Hi,

I am trying to access top level method from the class definition in  
IronRuby scripts. Example:

def global\_hi  
end

class A  
def hi  
global\_hi  
end  
end

A.new.hi

The script executed in ScriptEngine throws a NoSuchMethodError.  
I tried to ran the same code in iirb.exe It seems to behave the way I  
expected. But when I tried it in ir.exe, it throws the exception as  
well.  
I investigate a little more and found out

irb(main):021:0\> self.method(:global\_hi)  
=\> #\<Method: Object#global\_hi\>  
and  
in ir.exe as well as script engine

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

Is there anyway to make the script behave like the one in iirb.exe?

Thanks,  
Lewis

---

<div class="post-metadata">

**Author:** ![Lewis\_Lin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ea666f/32.png) [@Lewis\_Lin](https://www.ruby-forum.com/u/Lewis_Lin)\
**Post date:** [July 20, 2011, 4:49pm UTC](https://www.ruby-forum.com/t/access-top-level-methods/208944/3 "2011-07-20T16:49:58Z")

</div>

Hi,

Thanks for the reply. My main development version is on 1.1 for .NET  
3.5. However, I did try it on 1.1.3 on .NET 4.0.30319.225 and still are  
able to reproduce the issue. I am running Windows XP 32bit environment.  
Is there any environment variables I should look into? Here is the  
screen copy I just did.

As far as I understand, the global environment is actually a ‘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 ScriptEngine behaves differently.

Thanks for your time!  
Lewis

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\>

---

<div class="post-metadata">

**Author:** ![Lewis\_Lin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ea666f/32.png) [@Lewis\_Lin](https://www.ruby-forum.com/u/Lewis_Lin)\
**Post date:** [August 2, 2011, 9:19pm UTC](https://www.ruby-forum.com/t/access-top-level-methods/208944/4 "2011-08-02T21:19:30Z")

</div>

Hi,

I am doing this to implement a DSL. I want to allow user to define some  
function and be used by the DSL. Without this, I don’t know how to get  
it working. I am sure there is a rational explanation for what I see.  
Could someone help me please?

Thanks,  
Lewis
