# Code Review: ClrMemberEnumeration

**URL:** <https://www.ruby-forum.com/t/code-review-clrmemberenumeration/170076>\
**Category:** IronRuby\
**Created:** [June 12, 2009, 12:13am UTC](https://www.ruby-forum.com/t/code-review-clrmemberenumeration/170076 "2009-06-12T00:13:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [June 12, 2009, 12:13am UTC](https://www.ruby-forum.com/t/code-review-clrmemberenumeration/170076/1 "2009-06-12T00:13:48Z")

</div>

tfpt review “/shelveset:ClrMemberEnumeration;REDMOND\tomat”

Implements CLR member enumeration: methods Module#methods,  
instance\_methods, etc. now include CLR member names.

Adds IronRuby::Clr::Name class, which represents a pair of names of a  
CLR method - the primary name is mangled (Ruby name) the alternative  
name is the actual CLR name.

Reflection methods like Module#instance\_methods return instances of  
ClrName whenever a CLR member is encountered that could be called by  
both names. ClrName has methods to\_s, to\_sym, to\_str, \<=\>, inspect,  
dump so that it can be used wherever a string can be used. The display  
string for the name uses single quotes so that you can easily  
distinguish CLR (dual) names from regular names (plain mutable strings).

> > > System::Byte.instance\_methods(false)  
> > > =\> ["-", “%”, “&”, “\*”, “\*\*”, “/”, “-@”, “[]”, “^”, “|”, “~”, “+”, “\<”,  
> > > “\<\<”, “\<=”, “\<=\>”, “==”, “\>”, “\>=”, “\>\>”, “abs”,  
> > > “div”, “divmod”, “modulo”, “quo”, “to\_f”, “to\_s”, “zero?”, “size”,  
> > > ‘compare\_to’, ‘equals’, ‘get\_hash\_code’, ‘to\_string’, ‘get\_type\_code’]
> 
> > > l = System::Byte.instance\_methods(false).last  
> > > =\> ‘get\_type\_code’
> 
> > > l.ruby\_name  
> > > =\> “get\_type\_code”
> 
> > > l.clr\_name  
> > > =\> “GetTypeCode”

Now this works with meta-programming as well:

class System::Decimal  
instance\_methods(false).each do |name|  
mangled = ‘\_\_’ + name

```
alias_method(mangled, name)
private mangled

define_method(name) do |*args|
  puts "method called: #{name}"
  send mangled, *args
end

```

end  
end

x, y = System::Decimal.new(1), System::Decimal.new(2)  
p x + y # =\> “method called: +”  
p x.CompareTo(y) # =\> “method called: compare\_to”

The trick here is in new set of define\_method overloads strongly typed  
to ClrName that define the real method using the ruby\_name and alias it  
using the clr\_name. So both CompareTo and compare\_to calls are  
intercepted.

We might add similar overloads to other methods to improve  
meta-programming experience for CLR types when needed.

Note that instance\_methods never returns any ClrNames for a built-in  
class or module, although some built-ins expose CLR members. The reason  
is that built-ins are restricted to do no name-mangling in order to  
prevent conflicts with Ruby. The CLR methods are only exposed via their  
actual names.

> > > Thread.instance\_methods(false).sort  
> > > =\> [“Abort”, “ApartmentState”, “ApartmentState=”, “CurrentCulture”,  
> > > “CurrentCulture=”, “CurrentUICulture”, “CurrentUICul  
> > > ture=”, “ExecutionContext”, “GetApartmentState”, “GetCompressedStack”,  
> > > “GetHashCode”, “Interrupt”, “IsAlive”, “IsBackgro  
> > > und”, “IsBackground=”, “IsThreadPoolThread”, “Join”, “ManagedThreadId”,  
> > > “Name”, “Name=”, “Priority”, “Priority=”, “Resum  
> > > e”, “SetApartmentState”, “SetCompressedStack”, “Start”, “Suspend”,  
> > > “ThreadState”, “TrySetApartmentState”, “[]”, “[]=”, "  
> > > abort\_on\_exception", “abort\_on\_exception=”, “alive?”, “exit”, “group”,  
> > > “inspect”, “join”, “key?”, “keys”, “kill”, "raise  
> > > ", “run”, “status”, “stop?”, “terminate”, “value”, “wakeup”]

Tomas
