动态调用动态语言,第 2 部分: 在运行时寻找、执行和修改脚本
2009-11-19 00:00:00 来源:WEB开发网在 Ruby 脚本中访问共享的 Java 对象时,一定要记住 Ruby 的全局变量语法。如果省略了全局变量前面的 $ 符号,那么 JRuby 1.0 和当前的 JRuby 1.0.1 二进制版本会抛出一个 RaiseException,而且不提供错误的相关信息。JRuby 源代码存储库中已经纠正了这个 bug,所以在以后的二进制版本中应该不会出现这个问题。
如清单 6 所示,在 Ruby 脚本中,需要在变量名前面加上 $ 符号,这样才能访问放在脚本引擎范围内的共享 Java 对象。这是 Ruby 的全局变量语法。脚本引擎以全局变量的形式向脚本共享 Java 对象,所以必须使用 Ruby 的全局变量语法。
还要注意,在调用共享的 Java 对象时,JRuby 会自动地将 Ruby 式代码转换为 Java 式代码。例如,如果 JRuby 发现代码按照 Ruby 命名约定(即以下划线分隔单词)调用 Java 对象上的方法,比如 $result.product_name = 'Ruby Mortgage',那么 JRuby 会寻找不带下划线的大小写混合式方法名。因此,Ruby 式方法名 product_name= 会正确地转换为 Java 调用 result.setProductName("Ruby Mortgage")。
程序输出
现在用这三个抵押产品脚本文件运行 ScriptMortgageQualifierRunner 程序,看看它的输出。可以使用源代码下载文件中的 Ant 脚本运行这个程序。如果喜欢使用 Maven,那么可以按照 ZIP 文件中的 README.txt 文件中的说明用 Maven 构建并运行这个程序。Ant 命令是 ant run。run 任务确保脚本引擎和语言 JAR 文件在类路径中。清单 7 给出 Ant 的输出。
清单 7. Ant 产生的程序输出> ant run
Buildfile: build.xml
compile:
[mkdir] Created dir: C:\temp\script-article\build-main\classes
[javac] Compiling 10 source files to C:\temp\script-article\build-main\classes
run:
[java] Processing file: GroovyMortgage.groovy
[java] Borrower: Good Borrower
[java] Credit score: 800
[java] Sales price: 300000.0
[java] Down payment: 60000.0
[java] * Mortgage product: Groovy Mortgage, Qualified? true
[java] * Interest rate: 0.06
[java] * Message: Groovy! You qualify.
[java] Processing file: JavaScriptFirstTimeMortgage.js
[java] Borrower: Good Borrower
[java] Credit score: 800
[java] Sales price: 300000.0
[java] Down payment: 60000.0
[java] * Mortgage product: JavaScript FirstTime Mortgage, Qualified? false
[java] * Interest rate: 0.0
[java] * Message: Only first-time home buyers qualify for this mortgage.
[java] Processing file: RubyPrimeMortgage.rb
[java] Borrower: Good Borrower
[java] Credit score: 800
[java] Sales price: 300000.0
[java] Down payment: 60000.0
[java] * Mortgage product: Ruby Mortgage, Qualified? true
[java] * Interest rate: 0.06
[java] * Message: Qualified!
[java] Processing file: GroovyMortgage.groovy
[java] Borrower: Average Borrower
[java] Credit score: 700
[java] Sales price: 300000.0
[java] Down payment: 60000.0
[java] * Mortgage product: Groovy Mortgage, Qualified? true
[java] * Interest rate: 0.06
[java] * Message: Groovy! You qualify.
[java] Processing file: JavaScriptFirstTimeMortgage.js
[java] Borrower: Average Borrower
[java] Credit score: 700
[java] Sales price: 300000.0
[java] Down payment: 60000.0
[java] * Mortgage product: JavaScript FirstTime Mortgage, Qualified? false
[java] * Interest rate: 0.0
[java] * Message: Monthly salary of $4500 exceeds the $4,000 maximum.
[java] Processing file: RubyPrimeMortgage.rb
[java] Borrower: Average Borrower
[java] Credit score: 700
[java] Sales price: 300000.0
[java] Down payment: 60000.0
[java] * Mortgage product: Ruby Mortgage, Qualified? true
[java] * Interest rate: 0.065
[java] * Message: Qualified!
[java] Processing file: GroovyMortgage.groovy
[java] Borrower: Investor Borrower
[java] Credit score: 720
[java] Sales price: 300000.0
[java] Down payment: 30000.0
[java] * Mortgage product: Groovy Mortgage, Qualified? true
[java] * Interest rate: 0.06
[java] * Message: Groovy! You qualify.
[java] Processing file: JavaScriptFirstTimeMortgage.js
[java] Borrower: Investor Borrower
[java] Credit score: 720
[java] Sales price: 300000.0
[java] Down payment: 30000.0
[java] * Mortgage product: JavaScript FirstTime Mortgage, Qualified? false
[java] * Interest rate: 0.0
[java] * Message: This mortgage is not intended for investors.
[java] Processing file: RubyPrimeMortgage.rb
[java] Borrower: Investor Borrower
[java] Credit score: 720
[java] Sales price: 300000.0
[java] Down payment: 30000.0
[java] * Mortgage product: Ruby Mortgage, Qualified? false
[java] * Interest rate: 0.0
[java] * Message: Down payment must be at least 20% of sale price.
[java] Processing file: GroovyMortgage.groovy
[java] Borrower: Risk E. Borrower
[java] Credit score: 520
[java] Sales price: 300000.0
[java] Down payment: 10000.0
[java] * Mortgage product: Groovy Mortgage, Qualified? false
[java] * Interest rate: 0.0
[java] * Message: Down payment of 3.33% is insufficient. 5% minimum required.
[java] Processing file: JavaScriptFirstTimeMortgage.js
[java] Borrower: Risk E. Borrower
[java] Credit score: 520
[java] Sales price: 300000.0
[java] Down payment: 10000.0
[java] * Mortgage product: JavaScript FirstTime Mortgage, Qualified? true
[java] * Interest rate: 0.08
[java] * Message: Congratulations, you qualify.
[java] Processing file: RubyPrimeMortgage.rb
[java] Borrower: Risk E. Borrower
[java] Credit score: 520
[java] Sales price: 300000.0
[java] Down payment: 10000.0
[java] * Mortgage product: Ruby Mortgage, Qualified? false
[java] * Interest rate: 0.0
[java] * Message: Credit score of 520 is lower than 700 minimum
[java] Sleeping for one minute before reprocessing files.
[java] Use Ctrl-C to exit...
更多精彩
赞助商链接