`
prec
  • 浏览: 63406 次
  • 性别: Icon_minigender_1
  • 来自: 互联网
最近访客 更多访客>>
社区版块
存档分类
最新评论

IronRuby初探——在C#中调用Ruby代码

阅读更多

首先引入相关DLL:Microsoft.Scripting.dll 和Ruby.dll

然后我们新建一个类:

public class Class1
    
...{
        
public ScriptModule test(string path)
        
...{
           
            SourceUnit unit;

            
string name = "rubytest1";
            unit 
= new SourceFileUnit(RubyEngine.CurrentEngine,path, name, Encoding.UTF8);
            

            ScriptModule m 
= unit.CompileToModule();
            
            m.Execute();
            
return m;
        }

    }

然后,我们就可以通过,下面的代码执行RUBY文件的代码,并查看结果了:

protected void Page_Load(object sender, EventArgs e)
    
...{
        StringBuilder builder 
= new StringBuilder();
      
        
using (StringWriter output = new StringWriter(builder))
        
...{
            TextWriter console_out 
= Console.Out;
            ScriptEnvironment.GetEnvironment().RedirectIO(
null, output, null);
            
try
            
...{
                
new Class1().test(@"F:\test.rb");
            }

            
finally
            
...{
                ScriptEnvironment.GetEnvironment().RedirectIO(
null, console_out, null);
            }

        }

        
string actualOutput = builder.ToString();
        Response.Write(actualOutput);
        
    }

比如test.rb:

puts "test"

这个是最简答的RUBY代码了,我们也可以结合.NET CLR,比如:

require 'mscorlib'

= System::Text::StringBuilder.new
b.Append 
1
b.Append 
'-'
b.Append true
puts b.to_string
puts b.length

这反应了IronRuby的特色。

 

当然,我们也可以在代码里执行指定的代码而不是从文件:

public void test2() ...{
            ScriptModule module 
= ScriptDomainManager.CurrentManager.CreateModule("rubytest2");
            module.SetVariable(
"test""this is a test");

            RubyEngine.CurrentEngine.Execute(
"puts test", module);

}

 

protected void Page_Load(object sender, EventArgs e)
    
...{
        StringBuilder builder 
= new StringBuilder();
      
        
using (StringWriter output = new StringWriter(builder))
        
...{
            TextWriter console_out 
= Console.Out;
            ScriptEnvironment.GetEnvironment().RedirectIO(
null, output, null);
            
try
            
...{
                
new Class1().test2();
            }

            
finally
            
...{
                ScriptEnvironment.GetEnvironment().RedirectIO(
null, console_out, null);
            }

        }

        
string actualOutput = builder.ToString();
        Response.Write(actualOutput);
        
    }

上面实现了动态建立了一段代码并且从C#给RUBY代码赋值,并显示的过程。

不只是可以输出结果,我们也可以动态地监视他们:

object val = module.LookupVariable("test");

我们也可以直接来执行表达式:

object x = RubyEngine.CurrentEngine.Evaluate("1 + 1");

或者以一种IRB的交互方式:

object x = RubyEngine.CurrentEngine.ExecuteInteractiveCode("1+1");

好了,就简单地介绍到这里,更多更复杂的应用还要等待我们的正式版的推出。

分享到:
评论

相关推荐

    [IronRuby] C# 4.0调用ActiveRecord

    NULL 博文链接:https://raylinn.iteye.com/blog/670006

    Ruby中文文档.zip

    Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada以及 Lisp ...

    IronRuby开发包(Pre Alpha1)

    IronRuby 是 Ruby 编程语言在微软 .NET 平台上的一种实现。 IronRuby 与 Wilco Bauwer 的 IronRuby project 同名,2007年4月30日由微软于 MIX 上发表。并计划于2007年的 OSCON 公开推出。 2007年8月31日,John Lam...

    ASP.NET源码——[博客空间]IronRuby博客中文版 Alpha.zip

    ASP.NET源码——[博客空间]IronRuby博客中文版 Alpha.zip

    Ruby入门到精通

    Ruby入门到精通,Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel...

    ruby-1.8.7-p302.tar.gz

    Ruby,一种为简单快捷的面向对象编程(面向对象程序设计)而创的脚本语言,在20世纪90年代由日本人松本行弘(まつもとゆきひろ/Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、...

    ruby最新版稳定版

    Ruby,一种为简单快捷的面向对象编程(面向对象程序设计)而创的脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、...

    ruby安装包,window安装包

    Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada以及 Lisp ...

    ruby-2.6.6.1.rar

    一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada以及 Lisp 语言。...

    IronRuby博客中文版_多用户博客空间源码下载.rar

    IronRuby博客中文版_多用户博客空间源码下载

    ruby 2.4.4 X64

    Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada以及 Lisp ...

    ruby-1.9.3-p0-doc-chm

    Ruby,一种为简单快捷的面向对象编程(面向对象程序设计)而创的脚本语言,在20世纪90年代由日本人松本行弘(まつもとゆきひろ/Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、...

    Ruby中文文档

    Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada以及 Lisp ...

    Rails for .NET Developers (Facets of Ruby)

    We start with a gentle introduction to the object-oriented Ruby language with examples and direct comparisons to C#, so you'll quickly feel at home writing Ruby code for the first time. We then get ...

    [博客空间]IronRuby博客中文版 Alpha_ironruby-pre-alpha1(ASP.NET源码).rar

    [博客空间]IronRuby博客中文版 Alpha_ironruby-pre-alpha1(ASP.NET源码).rar

    ironruby-pre-alpha1.zip_pre

    Ruby语言在.net平台的实现。alpha版源代码,最初阶段代码,可能比较粗糙,但是对于初学者确实比较容易入门。

    Sams.IronRuby.Unleashed.Feb.2010.pdf

    Sams.IronRuby.Unleashed.Feb.2010.pdf

    Sams.IronRuby.Unleashed.Feb.2010.rar

    Sams.IronRuby.Unleashed.Feb.2010.rar

    Nehul Singh IDE for .Net:用于 DOT NET 编程的 NS-ORG IDE-开源

    另请访问官网 nehulsinghide.blogspot.in 使用包括 C#、VB.NET、F#、IronPython 和 ...它附带了许多代码完成和代码片段的新功能插入,剪贴板映射,最棒的是它可以转换 VB.Net-Python、BOO、C#、Ruby 的代码,反之亦然。

Global site tag (gtag.js) - Google Analytics