IronRuby 1.0及与.NET互操作

来源:互联网 发布:jre 7u9 windows x32 编辑:程序博客网 时间:2024/05/16 19:51

ironruby 是.Net平台下的一个ruby实现,众所周知,ruby具有Perl的灵活性,带有实体对象模型,是一门动态/解释语言。从进入ironruby 0.9.1之后,ironruby日趋稳定,目前ironruby的最新版本是0.9.2.
由于IronRuby是与.NET集成在一起的,因此你可以在IronRuby中调用任何已有的.NET代码。这意味着可以在IrongRuby 中使用任何框架,比如Windows Forms、WPF或者GTK(#),因为.NET和Mono(分)拥有对这些框架的CLI绑定。Mono甚至有一个使用GTK实现的Windows Forms,这样应用程序无须修改就可以运行在两个实现上。
动态语言与静态语言的互操作:
IronRuby带来的改变:
1. 与静态语言的互操作:IronRuby与.NET框架集成的非常紧密,在IronRuby中调用C#/VB代码不会感觉是在使用“互操作”。C#也可以通过DLR Hosting API调用IronRuby代码。而在.NET 4.0中,动态方法分配已经成为了C#的一部分,因此在C#中调用IronRuby代码和调用C#方法差不多。由于IronRuby基于DLR,因此也可以方便的与其它DLR语言进行交互,如今Python和Ruby可以很好的合作,未来的DLR语言也一样可以。
2. 更稳定且丰富的支持,你可以抛弃rubyforge上许多质量不高的无人维护的gems,改用.Net自身丰富的资源,你可以放弃ruby中频繁变更的 win32api,而使用.net的p/invoke,你可以放弃无人维护的fireruby,而简单用.net firebird provider为自己添加firebird的activerecord支持而不用再担心找不到数据库的activerecord支持。
3. 为.Net带来Rails,基于Ironruby 0.9.2你只需要对rails 2.3.4做一些小小的修改,就可以再CLR/DLR上运行最新的Rails,同时使用Ironruby也可以为.Net的MVC框架带了许多新的变化,这些在后面会详述。
运行在ironruby上的rails 2.3.4

这里我们主要讨论ironruby与.Net的集成问题。
要在ironruby中使用.net的装配件,首先必须引用装配件,根据不同的装配件,ironruby提供了三种不同方法
1. 第三方的装配件,只要它存在于ironruby的LOAD_PATH中,你就可以直接引用dll的名字。

Ruby代码

  1. require "modelxxx"
require "modelxxx"

2.GAC内的装配件,可以用强命名引用。

Ruby代码

  1. require "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
require "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

3. 使用ironruby自带的CLR组件,则可以直接引用。

Ruby代码

  1. require "System.Windows.Forms"
require "System.Windows.Forms"

下面的"Hello,World"展示了IronRuby和.NET是如何集成的

Ruby代码

  1. require "System"
  2. include System 
  3. Console.Out.WriteLine("Hello,World") 
  4. Console.Out.write_line("Hello,World") 
require "System"include SystemConsole.Out.WriteLine("Hello,World")Console.Out.write_line("Hello,World")

Ironruby为.Net的方法创建了ruby风格的别名,所以WriteLine和write_line是等价的。
这是一个复杂一点例子,Ironruby使用了.Net的Winform来作为GUI,这段例子与大部分.NET winform的代码结构是一样,目的只是为了展示如何使用Winform,实际过程中,除非是需要动态生成ui,否则你应该在.NET中完成form的设计,然后在ironruby中直接使用。

Java代码

  1. require 'mscorlib'
  2. require 'System.Windows.Forms'
  3. require 'System.Drawing'
  4. Application = System::Windows::Forms::Application   
  5. Form = System::Windows::Forms::Form   
  6. MessageBox = System::Windows::Forms::MessageBox   
  7. Button = System::Windows::Forms::Button   
  8. Point = System::Drawing::Point   
  9. class MyForm < Form   
  10.    def initialize   
  11.      self.text = "My .NET Form from Ruby"
  12. @button = Button.new
  13. @button.location = Point.new 150, 150
  14. @button.text = "Click Me!"
  15.      my_click_handler = Proc.new {|sender, e| MessageBox.show 'Hello from Ruby!'}   
  16. @button.click(&my_click_handler)   
  17.      self.controls.add @button
  18.    end   
  19. end   
  20. my_form = MyForm.new
  21. Application.run my_form  
require 'mscorlib'  require 'System.Windows.Forms'require 'System.Drawing'     Application = System::Windows::Forms::Application  Form = System::Windows::Forms::Form  MessageBox = System::Windows::Forms::MessageBox  Button = System::Windows::Forms::Button  Point = System::Drawing::Point      class MyForm < Form        def initialize       self.text = "My .NET Form from Ruby"          @button = Button.new       @button.location = Point.new 150, 150       @button.text = "Click Me!"          my_click_handler = Proc.new {|sender, e| MessageBox.show 'Hello from Ruby!'}       @button.click(&my_click_handler)          self.controls.add @button     end   end      my_form = MyForm.new   Application.run my_form 

为了证明Ironruby的良好扩展性,这个是我机器上另一份rails的截图,我用SQLite for ADO.NET在一个小时里,写了SQLite的activerecord_adapter,并正常工作。

原帖:http://www.javaeye.com/topic/524336