Calling a C# static from IronRuby

Hi all,
Is there any way to declare a C# static method accessible from IronRuby
as a regular Ruby method?
like:
public class MyCSharpClass {
public static string mymethod(string test) {
return test + “yes”;
}
}

in ironruby:
puts mymethod(“test”)

I guess you can do that in two ways.

The first one is to add a statement in Ruby code:

class Object
def mymethod(str)
MyCSharpClass.mymethod(str)
end
end

Or you can write an IronRuby extension in C# and make that class extend
the
Object class.

Shay.

Shay F.
Author of IronRuby Unleashed
http://www.IronShay.com
Follow me: http://twitter.com/ironshay

You can only reach it by adding the class as receiver.

but you can use method missing as a dispatcher:

alias :old_mm :method_missing
def method_missing(method_name, *args, &block)
return MyCSharpClass.send(method_name, *args) if
MyCSharpClass.respond_to?
method_name
old_mn method_name, *args, &block
end

try this in a console :slight_smile:

alias :old_mm :method_missing
def method_missing(method_name, *args, &block)
return System::Math.send(method_name, *args) if
System::Math.respond_to?
method_name
old_mn method_name, *args, &block
end

puts pi

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Google Wave: [email protected]
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

FYI, we’re thinking about allowing you to use “include” with .NET types,
which will include it’s static methods. That would enable:

include MyCSharpClass
puts mymethod “foo”

IronPython already does this for import, so it seems like a good idea:

from MyCSharpClass import mymethod
print mymethod(“foo”)

~Jimmy

From: [email protected]
[mailto:[email protected]] On Behalf Of Ivan Porto
Carrero
Sent: Friday, November 13, 2009 11:04 AM
To: [email protected]
Subject: Re: [Ironruby-core] Calling a C# static from IronRuby

You can only reach it by adding the class as receiver.

but you can use method missing as a dispatcher:

alias :old_mm :method_missing
def method_missing(method_name, *args, &block)
return MyCSharpClass.send(method_name, *args) if
MyCSharpClass.respond_to? method_name
old_mn method_name, *args, &block
end

try this in a console :slight_smile:

alias :old_mm :method_missing
def method_missing(method_name, *args, &block)
return System::Math.send(method_name, *args) if
System::Math.respond_to? method_name
old_mn method_name, *args, &block
end

puts pi

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Google Wave:
[email protected]mailto:[email protected]
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

On Fri, Nov 13, 2009 at 7:22 PM, Alexandre M.
<[email protected]mailto:[email protected]> wrote:
Hi all,
Is there any way to declare a C# static method accessible from IronRuby
as a regular Ruby method?
like:
public class MyCSharpClass {
public static string mymethod(string test) {
return test + “yes”;
}
}

in ironruby:
puts mymethod(“test”)

Posted via http://www.ruby-forum.com/.


Ironruby-core mailing list
[email protected]mailto:[email protected]
http://rubyforge.org/mailman/listinfo/ironruby-core

Ivan Porto carrero wrote:

You can only reach it by adding the class as receiver.

but you can use method missing as a dispatcher:

alias :old_mm :method_missing
def method_missing(method_name, *args, &block)
return MyCSharpClass.send(method_name, *args) if
MyCSharpClass.respond_to?
method_name
old_mn method_name, *args, &block
end

Thanks, it’s a nice workaround, i didn’t know about the
“method_missing”. Is there any way to do it C#, through the DLR (i
didn’t see anything about this, as it’s obviously language dependent) or
custom IronRuby classes? The reason is i have a lots of method that
would be called this way and i would like to use the most efficient way
to do it.

On a similar subject, is there a way to resolve dynamic variable at
runtime? Like, a user type a variable that was not declared, but the DLR
host is able to create on the fly an object for this variable and return
it. Is it possible? Is “IDynamicMetaObjectProvider” and
“IAttributesCollections” on CreateScope() are used for that?

FYI, we’re thinking about allowing you to use “include” with .NET types,
which will include it’s static methods. That would enable:
include MyCSharpClass
puts mymethod “foo”

Good, i used this similar method in IronPython, so IronRuby probably
deserve this kind of static import!

Thanks for your quick response.

My 2c on the matter

I think Ruby on .NET is great and stuff like the clr_new, overloads etc
are
a necessary evil to ease working with CLR classes.
But I do think that changing a basic construct like include will not be
good
unless the other rubies also include it. The reason for it is you only
use
clr_new (which is aptly prefixed btw) or overload etc when you’re
working
with the CLR but include you use in other implementations too and then
it
won’t behave consistently across the board.


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Google Wave: [email protected]
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

On Fri, Nov 13, 2009 at 9:07 PM, Jimmy S. <

On Fri, Nov 13, 2009 at 9:07 PM, Jimmy S. <[email protected]

wrote:
FYI, we’re thinking about allowing you to use “include” with .NET
types, which will include it’s static methods. That would enable:

I’d be very much in favour of this. A .NET static class full of static
methods always seemed like it would be a good map to a ruby Module to
me.

Whether you want to limit the ‘include’ functionality to only work
with static classes, or just work for static methods on any old class
I think is up for debate.
I’d go for ‘any old class’, but I generally fall on the ‘be as
permissive as possible’ side of the fence, so others may not agree :slight_smile:

On 14/11/2009, at 9:29 AM, Ivan Porto C. wrote:

I think Ruby on .NET is great and stuff like the clr_new, overloads
etc are a necessary evil to ease working with CLR classes.
But I do think that changing a basic construct like include will not
be good unless the other rubies also include it. The reason for it
is you only use clr_new (which is aptly prefixed btw) or overload
etc when you’re working with the CLR but include you use in other
implementations too and then it won’t behave consistently across the
board.

While consistency with other Ruby implementations is obviously
important, this is a CLR interop feature and shouldn’t affect the
normal ruby behavior of include, so I don’t see how it affects
consistency with other implementations at all?. It seems similar to
being able to use include on .NET namespaces from IronRuby, which is
of course also non-standard

On a similar subject, is there a way to resolve dynamic variable at
runtime? Like, a user type a variable that was not declared, but the DLR
host is able to create on the fly an object for this variable and return
it. Is it possible? Is “IDynamicMetaObjectProvider” and
“IAttributesCollections” on CreateScope() are used for that?

By the way, i’m trying to play with IDynamicMetaObjectProvider with
IronRuby0.92 and Visual Studio 2010 Beta 2 and i’m getting some
conflicts with System.Core… What are the reference assembly to include
to avoid such conflicts?

On Fri, Nov 13, 2009 at 1:06 PM, Shay F.
[email protected]wrote:

Maybe I’m missing something, but shouldn’t that be:

def mymethod(str)
MyCSharpClass*::*mymethod(str) # Use ‘::’ instead of ‘.’?
end

Ryan R.

Email: [email protected]
LinkedIn: http://www.linkedin.com/in/ryanriley
Blog: http://wizardsofsmart.net/
Website: http://panesofglass.org/

both work :: but in practice you see . more often though (it should also
be
SomeClass::new if we’re going down that route :))

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/82031?help-en
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/250948
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/250943

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Google Wave: [email protected]
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

“IAttributesCollections” on CreateScope() are used for that?

By the way, i’m trying to play with IDynamicMetaObjectProvider with
IronRuby0.92 and Visual Studio 2010 Beta 2 and i’m getting some conflicts
with System.Core… What are the reference assembly to include to avoid such
conflicts?

Here’s the current build specifically for .Net 4.0:
http://ironruby.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=33305

The conflicts you’re seeing between System.Core and
Microsoft.Scripting.Core are because all of Microsoft.Scripting.Core was
moved into System.Core for .NET 4.0. I think you can always alias
System.Core to use 0.9.2 in .NET 4.0, but you won’t able to take
advantage of any of the “dynamic” features.

~js

I thought ‘.’ was for instance and ‘::’ for static members. I need to
brush
up on my Ruby! :slight_smile:

Ryan R.

Email: [email protected]
LinkedIn: http://www.linkedin.com/in/ryanriley
Blog: http://wizardsofsmart.net/
Website: http://panesofglass.org/

FWIW I read too quickly before I sent the mail previous to this one.
Orion
put me straight, I have no objections to this and will put it in the
interop
features box.

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Google Wave: [email protected]
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)