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/.
Ivan Porto Carrero
2009-Nov-13 19:03 UTC
[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 :) 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 Carrero Blog: http://flanders.co.nz Google Wave: portocarrero.ivan at googlewave.com Twitter: http://twitter.com/casualjim Author of IronRuby in Action (http://manning.com/carrero) On Fri, Nov 13, 2009 at 7:22 PM, Alexandre Mutel <lists at ruby-forum.com>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 > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091113/4633b358/attachment.html>
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 Friedman Author of IronRuby Unleashed http://www.IronShay.com Follow me: http://twitter.com/ironshay On Fri, Nov 13, 2009 at 8:22 PM, Alexandre Mutel <lists at ruby-forum.com>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 > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091113/785b7f19/attachment.html>
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: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Ivan Porto Carrero Sent: Friday, November 13, 2009 11:04 AM To: ironruby-core at rubyforge.org 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 :) 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 Carrero Blog: http://flanders.co.nz Google Wave: portocarrero.ivan at googlewave.com<mailto:portocarrero.ivan at googlewave.com> Twitter: http://twitter.com/casualjim Author of IronRuby in Action (http://manning.com/carrero) On Fri, Nov 13, 2009 at 7:22 PM, Alexandre Mutel <lists at ruby-forum.com<mailto:lists at ruby-forum.com>> 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 Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org> http://rubyforge.org/mailman/listinfo/ironruby-core -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091113/78f78d7e/attachment-0001.html>
Ivan Porto Carrero
2009-Nov-13 20:29 UTC
[Ironruby-core] Calling a C# static from IronRuby
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 Carrero Blog: http://flanders.co.nz Google Wave: portocarrero.ivan at googlewave.com Twitter: http://twitter.com/casualjim Author of IronRuby in Action (http://manning.com/carrero) On Fri, Nov 13, 2009 at 9:07 PM, Jimmy Schementi < Jimmy.Schementi at microsoft.com> wrote:> 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:* ironruby-core-bounces at rubyforge.org [mailto: > ironruby-core-bounces at rubyforge.org] *On Behalf Of *Ivan Porto Carrero > *Sent:* Friday, November 13, 2009 11:04 AM > *To:* ironruby-core at rubyforge.org > *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 :) > > > > 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 Carrero > Blog: http://flanders.co.nz > Google Wave: portocarrero.ivan at googlewave.com > Twitter: http://twitter.com/casualjim > Author of IronRuby in Action (http://manning.com/carrero) > > > On Fri, Nov 13, 2009 at 7:22 PM, Alexandre Mutel <lists at ruby-forum.com> > 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 > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > > > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091113/9b5429aa/attachment.html>
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. -- Posted via http://www.ruby-forum.com/.
> 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? -- Posted via http://www.ruby-forum.com/.
> On Fri, Nov 13, 2009 at 9:07 PM, Jimmy Schementi <Jimmy.Schementi at microsoft.com > > 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 :-)> On 14/11/2009, at 9:29 AM, Ivan Porto Carrero 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091114/1d710c32/attachment.html>
> From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core- > bounces at rubyforge.org] On Behalf Of Alexandre Mutel > Sent: Friday, November 13, 2009 1:45 PM > To: ironruby-core at rubyforge.org > Subject: Re: [Ironruby-core] Calling a C# static from IronRuby > > > 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?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
On Fri, Nov 13, 2009 at 1:06 PM, Shay Friedman <shay.friedman at gmail.com>wrote:> 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 > >Maybe I''m missing something, but shouldn''t that be: def mymethod(str) MyCSharpClass*::*mymethod(str) # Use ''::'' instead of ''.''? end Ryan Riley Email: ryan.riley at panesofglass.org LinkedIn: http://www.linkedin.com/in/ryanriley Blog: http://wizardsofsmart.net/ Website: http://panesofglass.org/ -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091117/c4b1d798/attachment.html>
Ivan Porto Carrero
2009-Nov-17 17:29 UTC
[Ironruby-core] Calling a C# static from IronRuby
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 Carrero Blog: http://flanders.co.nz Google Wave: portocarrero.ivan at googlewave.com Twitter: http://twitter.com/casualjim Author of IronRuby in Action (http://manning.com/carrero) On Tue, Nov 17, 2009 at 4:29 PM, Ryan Riley <ryan.riley at panesofglass.org>wrote:> On Fri, Nov 13, 2009 at 1:06 PM, Shay Friedman <shay.friedman at gmail.com>wrote: > >> 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 >> >> > Maybe I''m missing something, but shouldn''t that be: > > def mymethod(str) > MyCSharpClass*::*mymethod(str) # Use ''::'' instead of ''.''? > end > > Ryan Riley > > Email: ryan.riley at panesofglass.org > LinkedIn: http://www.linkedin.com/in/ryanriley > Blog: http://wizardsofsmart.net/ > Website: http://panesofglass.org/ > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091117/3da085d6/attachment.html>
I thought ''.'' was for instance and ''::'' for static members. I need to brush up on my Ruby! :) Ryan Riley Email: ryan.riley at panesofglass.org LinkedIn: http://www.linkedin.com/in/ryanriley Blog: http://wizardsofsmart.net/ Website: http://panesofglass.org/ On Tue, Nov 17, 2009 at 11:29 AM, Ivan Porto Carrero <ivan at flanders.co.nz>wrote:> 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 Carrero > Blog: http://flanders.co.nz > Google Wave: portocarrero.ivan at googlewave.com > Twitter: http://twitter.com/casualjim > Author of IronRuby in Action (http://manning.com/carrero) > > > > On Tue, Nov 17, 2009 at 4:29 PM, Ryan Riley <ryan.riley at panesofglass.org>wrote: > >> On Fri, Nov 13, 2009 at 1:06 PM, Shay Friedman <shay.friedman at gmail.com>wrote: >> >>> 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 >>> >>> >> Maybe I''m missing something, but shouldn''t that be: >> >> def mymethod(str) >> MyCSharpClass*::*mymethod(str) # Use ''::'' instead of ''.''? >> end >> >> Ryan Riley >> >> Email: ryan.riley at panesofglass.org >> LinkedIn: http://www.linkedin.com/in/ryanriley >> Blog: http://wizardsofsmart.net/ >> Website: http://panesofglass.org/ >> >> _______________________________________________ >> Ironruby-core mailing list >> Ironruby-core at rubyforge.org >> http://rubyforge.org/mailman/listinfo/ironruby-core >> >> > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091117/a2904b10/attachment.html>
Ivan Porto Carrero
2009-Nov-19 08:26 UTC
[Ironruby-core] Calling a C# static from IronRuby
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 Carrero Blog: http://flanders.co.nz Google Wave: portocarrero.ivan at googlewave.com Twitter: http://twitter.com/casualjim Author of IronRuby in Action (http://manning.com/carrero) On Fri, Nov 13, 2009 at 10:50 PM, Orion Edwards <orion.edwards at gmail.com>wrote:> On Fri, Nov 13, 2009 at 9:07 PM, Jimmy Schementi < > Jimmy.Schementi at microsoft.com> 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 :-) > > > On 14/11/2009, at 9:29 AM, Ivan Porto Carrero 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 > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091119/7bffbed6/attachment.html>