Last active
July 4, 2017 08:00
-
-
Save dmikurube/2911dd4f43540192a8b4144c56ce0e7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'java' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
compile 'org.jruby:jruby-complete:9.1.5.0' | |
testCompile 'junit:junit:4.+' | |
} | |
sourceSets { | |
main { | |
java { | |
srcDir '.' | |
exclude 'Test*.java' | |
} | |
} | |
test { | |
java { | |
srcDir '.' | |
include 'Test*.java' | |
} | |
} | |
} | |
test { | |
testLogging { | |
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError' | |
} | |
} | |
task run(type:JavaExec) { | |
main = project.hasProperty('main') ? project.getProperty('main') : 'JRubyInstance' | |
classpath = sourceSets.main.runtimeClasspath | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts '' | |
puts 'Main $LOAD_PATH:' | |
$LOAD_PATH.each { |path| puts ' * ' + path.to_s } | |
puts 'Main $FOO : <%s>' % $FOO | |
# puts 'Main $BAR : <%s>' % $BAR | |
puts '' | |
puts ' Main> Appending to $LOAD_PATH' | |
puts ' Main> $LOAD_PATH << \'hogehoge\'' | |
$LOAD_PATH << 'hogehoge' | |
puts ' Main> Assigning to $FOO' | |
puts ' Main> $FOO = \'HOGE\'' | |
$FOO = 'HOGE' | |
puts '' | |
puts 'Main $LOAD_PATH:' | |
$LOAD_PATH.each { |path| puts ' * ' + path.to_s } | |
puts 'Main $FOO : <%s>' % $FOO | |
# puts 'Main $BAR : <%s>' % $BAR | |
puts '' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jruby.embed.LocalContextScope; | |
import org.jruby.embed.LocalVariableBehavior; | |
import org.jruby.embed.ScriptingContainer; | |
public class JRubyInstance { | |
public static void main(String[] args) { | |
/* | |
final ScriptingContainer container1 = new ScriptingContainer(LocalContextScope.SINGLETON); | |
container1.runScriptlet("puts 'Ruby1 $LOAD_PATH:'"); | |
container1.runScriptlet("$LOAD_PATH.each { |path| puts ' * ' + path.to_s }"); | |
container1.runScriptlet("puts 'Ruby1 $FOO : <%s>' % $FOO"); | |
container1.runScriptlet("puts 'Ruby1 $BAR : <%s>' % $BAR"); | |
container1.runScriptlet("puts ''"); | |
container1.runScriptlet("puts 'Ruby1> Assigning to BAR'"); | |
container1.runScriptlet("puts 'Ruby1> $BAR = \\'BAZ\\''"); | |
container1.runScriptlet("$BAR = 'BAZ'"); | |
container1.runScriptlet("puts ''"); | |
container1.runScriptlet("puts 'Ruby1 $LOAD_PATH:'"); | |
container1.runScriptlet("$LOAD_PATH.each { |path| puts ' * ' + path.to_s }"); | |
container1.runScriptlet("puts 'Ruby1 $FOO : <%s>' % $FOO"); | |
container1.runScriptlet("puts 'Ruby1 $BAR : <%s>' % $BAR"); | |
*/ | |
final String[] jrubyArgs = new String[1]; | |
jrubyArgs[0] = "global_variable.rb"; | |
org.jruby.Main.main(jrubyArgs); | |
final ScriptingContainer container2 = new ScriptingContainer(LocalContextScope.SINGLETON); | |
/* | |
container1.runScriptlet("puts 'Ruby1 $LOAD_PATH:'"); | |
container1.runScriptlet("$LOAD_PATH.each { |path| puts ' * ' + path.to_s }"); | |
container1.runScriptlet("puts 'Ruby1 $FOO : <%s>' % $FOO"); | |
container1.runScriptlet("puts 'Ruby1 $BAR : <%s>' % $BAR"); | |
*/ | |
container2.runScriptlet("puts 'Ruby2 $LOAD_PATH:'"); | |
container2.runScriptlet("$LOAD_PATH.each { |path| puts ' * ' + path.to_s }"); | |
container2.runScriptlet("puts 'Ruby2 $FOO : <%s>' % $FOO"); | |
// container2.runScriptlet("puts 'Ruby2 $BAR : <%s>' % $BAR"); | |
container2.runScriptlet("puts ''"); | |
container2.runScriptlet("puts 'Ruby2> Assigning to FOO'"); | |
container2.runScriptlet("puts 'Ruby2> $FOO = \\'QUX\\''"); | |
container2.runScriptlet("$FOO = 'QUX'"); | |
container2.runScriptlet("puts ''"); | |
/* | |
container1.runScriptlet("puts 'Ruby1 $LOAD_PATH:'"); | |
container1.runScriptlet("$LOAD_PATH.each { |path| puts ' * ' + path.to_s }"); | |
container1.runScriptlet("puts 'Ruby1 FOO : <%s>' % $FOO"); | |
container1.runScriptlet("puts 'Ruby1 BAR : <%s>' % $BAR"); | |
*/ | |
container2.runScriptlet("puts 'Ruby2 $LOAD_PATH:'"); | |
container2.runScriptlet("$LOAD_PATH.each { |path| puts ' * ' + path.to_s }"); | |
container2.runScriptlet("puts 'Ruby2 $FOO : <%s>' % $FOO"); | |
// container2.runScriptlet("puts 'Ruby2 $BAR : <%s>' % $BAR"); | |
} | |
} |
Author
dmikurube
commented
Jul 4, 2017
•
./gradlew run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
Main $LOAD_PATH:
* uri:classloader:/META-INF/jruby.home/lib/ruby/2.3/site_ruby
* uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib
Main $FOO : <>
Main> Appending to $LOAD_PATH
Main> $LOAD_PATH << 'hogehoge'
Main> Assigning to $FOO
Main> $FOO = 'HOGE'
Main $LOAD_PATH:
* uri:classloader:/META-INF/jruby.home/lib/ruby/2.3/site_ruby
* uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib
* hogehoge
Main $FOO : <HOGE>
Ruby2 $LOAD_PATH:
* uri:classloader:/META-INF/jruby.home/lib/ruby/2.3/site_ruby
* uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib
Ruby2 $FOO : <>
Ruby2> Assigning to FOO
Ruby2> $FOO = 'QUX'
Ruby2 $LOAD_PATH:
* uri:classloader:/META-INF/jruby.home/lib/ruby/2.3/site_ruby
* uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib
Ruby2 $FOO : <QUX>
BUILD SUCCESSFUL
Total time: 4.54 secs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment