Friday, March 30, 2007

Can Your Ruby Do This?

# scripting.rb

include Scriptable

x = 'Hello'
y = 'world'

groovy <<G
def z = 'multilanguage'
println("${x}, ${z} ${y}")
G


javascript <<JS
z = 'multilanguage'
println(x + ', ' + z + ' ' + y)
JS


# both => "Hello, multilanguage world!"

8 comments:

Anonymous said...

Cool! Do values go the other way, too? (e.g. JS to JRuby) And what types are supported?

Charles Oliver Nutter said...

The values only go one way, a bit like variables declared in a block are only visible within that block. So you could think of these script sections as being a call to a groovy block or a javascript block.

I think it could be possible to have the results come back out into the *same* variables though...hmmm.

Daniel Spiewak said...

I assume this is using the Java 6 scripting API?

Unknown said...

Yep, see the code here.

Anonymous said...

Really nice. But if it could go both ways it would be awesome.

paulk_asert said...

You can improve the Groovy example with:
println "$x, $y $z"

Davi Pires said...

now this is really really amazing!

Anonymous said...

Thanks for the nice post!