Slide 1

Slide 1 text

JRuby hacking guide David Calavera @calavera

Slide 2

Slide 2 text

+70 contributors

Slide 3

Slide 3 text

9 core members

Slide 4

Slide 4 text

30 single commits ~

Slide 5

Slide 5 text

we want you

Slide 6

Slide 6 text

core

Slide 7

Slide 7 text

rule of thumb

Slide 8

Slide 8 text

jruby-1.6.2 :003 > raise ArgumentError.new ArgumentError: ArgumentError from (irb):3:in `evaluate' from org/jruby/RubyKernel.java:1088:in `e from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:158:in `eval_input' from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:271:in `signal_status' from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:155:in `eval_input' from org/jruby/RubyKernel.java:1419:in `l from org/jruby/RubyKernel.java:1191:in `c from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:154:in `eval_input' from /Users/david/.rvm/rubies/jruby-1.6.2

Slide 9

Slide 9 text

do not freak out

Slide 10

Slide 10 text

ruby-1.8.7 :001 > raise ArgumentError.new ArgumentError: ArgumentError from (irb):1

Slide 11

Slide 11 text

jruby-1.6.2 :001 > raise ArgumentError.new ArgumentError: ArgumentError from (irb):1:in `evaluate' from org/jruby/RubyKernel.java:1088:in `e from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:158:in `eval_input' from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:271:in `signal_status' from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:155:in `eval_input' from org/jruby/RubyKernel.java:1419:in `l from org/jruby/RubyKernel.java:1191:in `c from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:154:in `eval_input' from /Users/david/.rvm/rubies/jruby-1.6.2

Slide 12

Slide 12 text

jruby-1.6.2 :001 > raise ArgumentError.new ArgumentError: ArgumentError from (irb):1:in `evaluate' from org/jruby/RubyKernel.java:1088:in `e from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:158:in `eval_input' from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:271:in `signal_status' from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:155:in `eval_input' from org/jruby/RubyKernel.java:1419:in `l from org/jruby/RubyKernel.java:1191:in `c from /Users/david/.rvm/rubies/jruby-1.6.2 irb.rb:154:in `eval_input' from /Users/david/.rvm/rubies/jruby-1.6.2

Slide 13

Slide 13 text

your ruby code is our test case

Slide 14

Slide 14 text

Test::Unit def test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable)) end

Slide 15

Slide 15 text

Rspec describe "glob file path" do it "lists contents of a file" do dir = Dir.new(@local_file_path) dir.entries.should include("junit.jar") end end

Slide 16

Slide 16 text

follow the specification

Slide 17

Slide 17 text

RubySpec describe "Dir.entries" do ... it "returns an Array of filenames in an existing directory including dotfiles" do ...

Slide 18

Slide 18 text

RubySpec describe "Dir.entries" do ... ruby_version_is "1.9" do it "calls #to_path on ..." do ...

Slide 19

Slide 19 text

RubySpec $ bin/jruby -S mspec \ spec/ruby/core/dir/entries_spec.rb $ bin/jruby --1.9 -S mspec \ spec/ruby/core/dir/entries_spec.rb

Slide 20

Slide 20 text

go to the source

Slide 21

Slide 21 text

ruby code static VALUE dir_entries(int argc, VALUE *argv, VALUE io) { VALUE dir; dir = dir_open_dir(argc, argv); return rb_ensure(rb_Array, dir, dir_close, dir); }

Slide 22

Slide 22 text

taming the beast

Slide 23

Slide 23 text

Test::Unit def test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable)) end

Slide 24

Slide 24 text

Test::Unit def test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable)) end

Slide 25

Slide 25 text

Ruby class $ vi src/org/jruby/RubyDir.java @JRubyClass(name = "Dir", include = "Enumerable") public class RubyDir extends RubyObject {

Slide 26

Slide 26 text

Ruby class $ vi src/org/jruby/RubyDir.java @JRubyClass(name = "Dir", include = "Enumerable") public class RubyDir extends RubyObject {

Slide 27

Slide 27 text

Ruby class $ vi src/org/jruby/RubyDir.java @JRubyClass(name = "Dir", include = "Enumerable") public class RubyDir extends RubyObject {

Slide 28

Slide 28 text

Ruby class $ vi src/org/jruby/RubyDir.java @JRubyClass(name = "Dir", include = "Enumerable") public class RubyDir extends RubyObject {

Slide 29

Slide 29 text

Ruby class $ vi src/org/jruby/RubyDir.java @JRubyClass(name = "Dir", include = "Enumerable") public class RubyDir extends RubyObject {

Slide 30

Slide 30 text

Test::Unit def test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable)) end

Slide 31

Slide 31 text

Test::Unit def test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable)) end

Slide 32

Slide 32 text

Ruby method @JRubyMethod(name = "mkdir", required = 1, optional = 1, meta = true, compat = RUBY1_8) public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {

Slide 33

Slide 33 text

Ruby method @JRubyMethod(name = "mkdir", required = 1, optional = 1, meta = true, compat = RUBY1_8) public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {

Slide 34

Slide 34 text

Ruby method @JRubyMethod(name = "mkdir", required = 1, optional = 1, meta = true, compat = RUBY1_8) public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {

Slide 35

Slide 35 text

Ruby method @JRubyMethod(name = "mkdir", required = 1, optional = 1, meta = true, compat = RUBY1_8) public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {

Slide 36

Slide 36 text

Ruby method @JRubyMethod(name = "mkdir", required = 1, optional = 1, meta = true, compat = RUBY1_8) public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {

Slide 37

Slide 37 text

Ruby method @JRubyMethod(name = "mkdir", required = 1, optional = 1, meta = true, compat = RUBY1_8) public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {

Slide 38

Slide 38 text

Ruby method @JRubyMethod(name = "mkdir", required = 1, optional = 1, meta = true, compat = RUBY1_8) public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {

Slide 39

Slide 39 text

Ruby method @JRubyMethod(name = "mkdir", required = 1, optional = 1, meta = true, compat = RUBY1_8) public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {

Slide 40

Slide 40 text

Ruby method @JRubyMethod(name = "mkdir", required = 1, optional = 1, meta = true, compat = RUBY1_8) public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {

Slide 41

Slide 41 text

Ruby method @JRubyMethod(name = "mkdir", required = 1, optional = 1, meta = true, compat = RUBY1_8) public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {

Slide 42

Slide 42 text

Test::Unit def test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable)) end

Slide 43

Slide 43 text

Test::Unit def test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable)) end

Slide 44

Slide 44 text

initialize @JRubyMethod(compat = RUBY1_8) public IRubyObject initialize(IRubyObject arg) private static final ObjectAllocator DIR_ALLOCATOR = new ObjectAllocator()

Slide 45

Slide 45 text

Test::Unit def test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable)) end

Slide 46

Slide 46 text

Test::Unit def test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable)) end

Slide 47

Slide 47 text

Ruby module $ vi src/org/jruby/RubyKernel.java @JRubyModule(name="Kernel") public class RubyKernel {

Slide 48

Slide 48 text

Ruby module @JRubyModule(name="Kernel") public class RubyKernel { $ vi src/org/jruby/RubyKernel.java

Slide 49

Slide 49 text

Ruby module $ vi src/org/jruby/RubyObject.java @JRubyClass(name="Object", include="Kernel") public class RubyObject extends ... {

Slide 50

Slide 50 text

Ruby module @JRubyMethod(name = {"kind_of?", "is_a?"}, required = 1) public static RubyBoolean kind_of_p (ThreadContext context, IRubyObject recv, IRubyObject type) {

Slide 51

Slide 51 text

Ruby module @JRubyMethod(name = {"kind_of?", "is_a?"}, required = 1) public static RubyBoolean kind_of_p (ThreadContext context, IRubyObject recv, IRubyObject type) {

Slide 52

Slide 52 text

Ruby module @JRubyMethod(name = {"kind_of?", "is_a?"}, required = 1) public static RubyBoolean kind_of_p (ThreadContext context, IRubyObject recv, IRubyObject type) {

Slide 53

Slide 53 text

learn more

Slide 54

Slide 54 text

by Hiroshi Nakamura http://tinyurl.com/ jruby-hacking-guide

Slide 55

Slide 55 text

by R.J. Lorimer http://tinyurl.com/ distilling-jruby

Slide 56

Slide 56 text

http://jruby.org

Slide 57

Slide 57 text

thank you!

Slide 58

Slide 58 text

Credits Charles Nutter’s Photo Booth http://www.flickr.com/photos/zpeckler/2648345658/ http://www.flickr.com/photos/dcmetroblogger/3298543398/ http://www.flickr.com/photos/jenny-pics/4520503357/