JRuby hacking guide

1,666 views
1,556 views

Published on

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,666
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
17
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

JRuby hacking guide

  1. 1. JRuby hacking guideDavid Calavera@calavera
  2. 2. +70contributors
  3. 3. 9coremembers
  4. 4. ~ 30singlecommits
  5. 5. we wantyou
  6. 6. core
  7. 7. rule ofthumb
  8. 8. jruby-1.6.2 :003 > raise ArgumentError.newArgumentError: ArgumentError from (irb):3:in `evaluate from org/jruby/RubyKernel.java:1088:in `e from /Users/david/.rvm/rubies/jruby-1.6.2irb.rb:158:in `eval_input from /Users/david/.rvm/rubies/jruby-1.6.2irb.rb:271:in `signal_status from /Users/david/.rvm/rubies/jruby-1.6.2irb.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.2irb.rb:154:in `eval_input from /Users/david/.rvm/rubies/jruby-1.6.2
  9. 9. do notfreak out
  10. 10. ruby-1.8.7 :001 > raise ArgumentError.newArgumentError: ArgumentError from (irb):1
  11. 11. jruby-1.6.2 :001 > raise ArgumentError.newArgumentError: ArgumentError from (irb):1:in `evaluate from org/jruby/RubyKernel.java:1088:in `e from /Users/david/.rvm/rubies/jruby-1.6.2irb.rb:158:in `eval_input from /Users/david/.rvm/rubies/jruby-1.6.2irb.rb:271:in `signal_status from /Users/david/.rvm/rubies/jruby-1.6.2irb.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.2irb.rb:154:in `eval_input from /Users/david/.rvm/rubies/jruby-1.6.2
  12. 12. jruby-1.6.2 :001 > raise ArgumentError.newArgumentError: ArgumentError from (irb):1:in `evaluate from org/jruby/RubyKernel.java:1088:in `e from /Users/david/.rvm/rubies/jruby-1.6.2irb.rb:158:in `eval_input from /Users/david/.rvm/rubies/jruby-1.6.2irb.rb:271:in `signal_status from /Users/david/.rvm/rubies/jruby-1.6.2irb.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.2irb.rb:154:in `eval_input from /Users/david/.rvm/rubies/jruby-1.6.2
  13. 13. your rubycode is ourtest case
  14. 14. Test::Unitdef test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable))end
  15. 15. Rspecdescribe "glob file path" do it "lists contents of a file" do dir = Dir.new(@local_file_path) dir.entries.should include("junit.jar") endend
  16. 16. followthespecification
  17. 17. RubySpecdescribe "Dir.entries" do ... it "returns an Array of filenames in anexisting directory including dotfiles" do ...
  18. 18. RubySpecdescribe "Dir.entries" do ... ruby_version_is "1.9" do it "calls #to_path on ..." do ...
  19. 19. 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
  20. 20. go to thesource
  21. 21. ruby codestatic VALUEdir_entries(int argc, VALUE *argv, VALUE io){ VALUE dir; dir = dir_open_dir(argc, argv); return rb_ensure(rb_Array, dir, dir_close, dir);}
  22. 22. tamingthe beast
  23. 23. Test::Unitdef test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable))end
  24. 24. Test::Unitdef test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable))end
  25. 25. Ruby class$ vi src/org/jruby/RubyDir.java@JRubyClass(name = "Dir", include = "Enumerable")public class RubyDir extends RubyObject {
  26. 26. Ruby class$ vi src/org/jruby/RubyDir.java@JRubyClass(name = "Dir", include = "Enumerable")public class RubyDir extends RubyObject {
  27. 27. Ruby class$ vi src/org/jruby/RubyDir.java@JRubyClass(name = "Dir", include = "Enumerable")public class RubyDir extends RubyObject {
  28. 28. Ruby class$ vi src/org/jruby/RubyDir.java@JRubyClass(name = "Dir", include = "Enumerable")public class RubyDir extends RubyObject {
  29. 29. Ruby class$ vi src/org/jruby/RubyDir.java@JRubyClass(name = "Dir", include = "Enumerable")public class RubyDir extends RubyObject {
  30. 30. Test::Unitdef test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable))end
  31. 31. Test::Unitdef test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable))end
  32. 32. Ruby method@JRubyMethod(name = "mkdir", required = 1,optional = 1, meta = true, compat = RUBY1_8)public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {
  33. 33. Ruby method@JRubyMethod(name = "mkdir", required = 1,optional = 1, meta = true, compat = RUBY1_8)public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {
  34. 34. Ruby method@JRubyMethod(name = "mkdir", required = 1,optional = 1, meta = true, compat = RUBY1_8)public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {
  35. 35. Ruby method@JRubyMethod(name = "mkdir", required = 1,optional = 1, meta = true, compat = RUBY1_8)public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {
  36. 36. Ruby method@JRubyMethod(name = "mkdir", required = 1,optional = 1, meta = true, compat = RUBY1_8)public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {
  37. 37. Ruby method@JRubyMethod(name = "mkdir", required = 1,optional = 1, meta = true, compat = RUBY1_8)public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {
  38. 38. Ruby method@JRubyMethod(name = "mkdir", required = 1,optional = 1, meta = true, compat = RUBY1_8)public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {
  39. 39. Ruby method@JRubyMethod(name = "mkdir", required = 1,optional = 1, meta = true, compat = RUBY1_8)public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {
  40. 40. Ruby method@JRubyMethod(name = "mkdir", required = 1,optional = 1, meta = true, compat = RUBY1_8)public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {
  41. 41. Ruby method@JRubyMethod(name = "mkdir", required = 1,optional = 1, meta = true, compat = RUBY1_8)public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {
  42. 42. Test::Unitdef test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable))end
  43. 43. Test::Unitdef test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable))end
  44. 44. initializeprivate static final ObjectAllocator DIR_ALLOCATOR = new ObjectAllocator()@JRubyMethod(compat = RUBY1_8)public IRubyObject initialize(IRubyObject arg)
  45. 45. Test::Unitdef test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable))end
  46. 46. Test::Unitdef test_dir_enumerable Dir.mkdir("./testDir_1") d = Dir.new("./testDir_1") assert(d.kind_of?(Enumerable))end
  47. 47. Ruby module$ vi src/org/jruby/RubyKernel.java@JRubyModule(name="Kernel")public class RubyKernel {
  48. 48. Ruby module$ vi src/org/jruby/RubyKernel.java@JRubyModule(name="Kernel")public class RubyKernel {
  49. 49. Ruby module$ vi src/org/jruby/RubyObject.java@JRubyClass(name="Object", include="Kernel")public class RubyObject extends ... {
  50. 50. Ruby module@JRubyMethod(name = {"kind_of?", "is_a?"}, required = 1)public static RubyBoolean kind_of_p(ThreadContext context, IRubyObject recv, IRubyObject type) {
  51. 51. Ruby module@JRubyMethod(name = {"kind_of?", "is_a?"}, required = 1)public static RubyBoolean kind_of_p(ThreadContext context, IRubyObject recv, IRubyObject type) {
  52. 52. Ruby module@JRubyMethod(name = {"kind_of?", "is_a?"}, required = 1)public static RubyBoolean kind_of_p(ThreadContext context, IRubyObject recv, IRubyObject type) {
  53. 53. learn more
  54. 54. http://tinyurl.com/jruby-hacking-guide by Hiroshi Nakamura
  55. 55. http://tinyurl.com/ distilling-jruby by R.J. Lorimer
  56. 56. http://jruby.org
  57. 57. thank you!
  58. 58. CreditsCharles Nutter’s Photo Boothhttp://www.flickr.com/photos/zpeckler/2648345658/http://www.flickr.com/photos/dcmetroblogger/3298543398/http://www.flickr.com/photos/jenny-pics/4520503357/

×