jRubyThe best of both worlds                      Christopher Spring
@autonomous
Confessions of a... Rubyist
Brief history of
Yukihiro “Matz” Matsumoto
Yukihiro “Matz” Matsumoto“I wanted a scripting language that was more powerful  than Perl, and more object-oriented than P...
“[We] need to focus on humans, on how humanscare about doing programming... We are the masters.               They are the...
“I hope to see Ruby help every programmer in     the world be productive, and to enjoy         programming, and to be happ...
A   primer
# booleanstruth = truelies = false# Stringsdude = Matzcredo = "#{dude} is nice,          so we are nice!"# Numbers42 ** 13...
# Arraysbackpack = [6, apple, [true, some_object]]# Hasheshash = {  true     => He loves me!,  dude     => Must obey!,  ba...
Wtf is a symbol?
coders = [  { language => ruby, location => johannesburg },  { language => java, location => port elizabeth },  # ...  { l...
Cool, what about    classes?
public Class Person {  private String firstName, lastName;    public Person(String firstName, String lastName){      setFi...
class Person  attr_accessor :first_name, :last_name  def initialize(first_name, last_name)    @first_name = first_name    ...
class Person   attr_accessor :first_name, :last_name   def initialize(first_name, last_name)     @first_name = first_name ...
Btw, everything is an      Object!
true.class() # => TrueClassfalse.to_s() # => "false"5.9.floor()   # => 53.times{ puts(Hello JUG!) }# => Hello JUG!# => Hel...
module Earthend
module Earth  class Person    attr_reader :first_name, :last_name    def initialize(first_name, last_name)      @first_nam...
module Earth  class Person    attr_reader :first_name, :last_name    def initialize(first_name, last_name)      @first_nam...
module RepublicPersonnel  def storm_trooper    Earth::Person.new(Storm, Trooper)  endendclass CloningVat  extend RepublicP...
module RepublicPersonnel  def storm_trooper    Earth::Person.new(Storm, Trooper)  endendclass CloningVat  extend RepublicP...
module TehForce  def sense    ... a disturbance in the force  endendmodule Earth  class Person    include TehForce  endend
module TehForce  def sense    ... a disturbance in the force  endendmodule Earth  class Person    include TehForce  endend...
def some_days( something_happens )  puts The sun rises  puts something_happens  puts The sun setsenddef other_days( this_h...
def some_days( something_happens )  puts The sun rises  puts something_happens  puts The sun setsend
def some_days( something_happens )  puts The sun rises  puts something_happens  puts The sun setsend
def some_days( something_happens )  puts The sun rises  puts something_happens  puts The sun setsenddef some_days  puts Th...
file = File.open(i <3 ruby.txt, w)file.puts "... i really do!"file.close
file = File.open(i <3 ruby.txt, w) file.puts "... i really do!" file.closeFile.open(i <3 ruby.txt, w) do |file|  file.puts...
Rubyists TATFT!describe Calculator do  before(:each) do    @calc = Calculator.new  end  context Addition do    it returns ...
Metaprogramming
class Person  def initialize    @height, @weight, @hair_color = 6.2, 70kg, brown  end  %w(height weight hair).each do |pro...
A   primer
or....
Demo time!!
Interactive Ruby $ jirb jruby-1.6.7 :001 > require java  => true jruby-1.6.7 :001 > #...
Ackermann
class Ackermann  def self.ack(m, n)    return n + 1           if m.zero?    return ack(m - 1, 1)   if n.zero?    ack( m - ...
include Rubeus::SwingJFrame.new(Ackerizer) do |frame|  frame.layout = java.awt.FlowLayout.new  @m, @n = JTextField.new(3),...
class Ackermann  def self.ack(m, n)    return n + 1           if m.zero?    return ack(m - 1, 1)   if n.zero?    ack( m - ...
class Ackermann  def self.ack(m, n)    return n + 1              if m.zero?    return ack(m - 1, 1)      if n.zero?    ack...
include Rubeus::SwingJFrame.new(Ackerizer) do |frame|  frame.layout = java.awt.FlowLayout.new  @m, @n = JTextField.new(3),...
Method overloadingpublic class OverloadTypeChecker {  public static String inspect(long value) {    return "long";  }    p...
require javajava_import OverloadTypeChecker do  OTCend# jRuby happily handles the simple caseOTC.inspect(5)           # lo...
public class BitLength {  public int neededFor(int i) {    return 32;  }    public int neededFor(long l) {      return 64;...
# jRuby can be forced to use a specific overloadrequire javajava_import BitLengthbits = BitLength.newbits.needed_for(1_000...
# jRuby can be forced to use a specific overloadrequire javajava_import BitLengthbits = BitLength.newbits.needed_for(1_000...
# jRuby can be forced to use a specific overloadrequire javajava_import BitLengthbits = BitLength.newbits.needed_for(1_000...
# jRuby can be forced to use a specific overloadrequire javajava_import BitLengthbits = BitLength.newbits.needed_for(1_000...
... and more about        types
public class TypeChecker{  public static String check(Object o){    return o.getClass().getName();  }}require javaruby_arr...
Sooo, what about those   C extensions... ?
Awesome, but why?• Cross platform GUI dev• Performance• Ruby interface to legacy code• Use a ruby test framework  (Serious...
import org.jruby.embed.InvokeFailedException;import org.jruby.embed.ScriptingContainer;public class RubyFromJava {  public...
More Demos!!
http://pragprog.com/book/jruby/using-jruby
http://www.meetup.com/Code-Coffee-JHB/
http://www.meetup.com/RubyOnBeer/
Questions?
jRuby: The best of both worlds
jRuby: The best of both worlds
jRuby: The best of both worlds
jRuby: The best of both worlds
jRuby: The best of both worlds
Upcoming SlideShare
Loading in...5
×

jRuby: The best of both worlds

2,358

Published on

An introduction to ruby and jruby with examples of accessing java from ruby, and ruby from java

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,358
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
21
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • 24 Feb 1993 Ruby was conceived\n25 Dec 1996 v 1.0 was released\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • If the content of the object is important, use a string\nIf identity is important, use a symbol\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Ruby is truly object oriented\n
  • Use modules as a namespace\n
  • Use modules as a namespace\n
  • Use modules as a namespace\n
  • Extend class level behaviour\n
  • Extend class level behaviour\n
  • Notice that there isn&amp;#x2019;t a first and last_name!!\n
  • Notice that there isn&amp;#x2019;t a first and last_name!!\n
  • See the repetition?\n
  • See the repetition?\n
  • See the repetition?\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Google summer of code: \nCommon C extension API entry points translated\nand compiled in Java Native Interface\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Checkout RubyOnBeer: http://www.meetup.com/RubyOnBeer/\n
  • \n
  • jRuby: The best of both worlds

    1. 1. jRubyThe best of both worlds Christopher Spring
    2. 2. @autonomous
    3. 3. Confessions of a... Rubyist
    4. 4. Brief history of
    5. 5. Yukihiro “Matz” Matsumoto
    6. 6. Yukihiro “Matz” Matsumoto“I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python...”
    7. 7. “[We] need to focus on humans, on how humanscare about doing programming... We are the masters. They are the slaves.”
    8. 8. “I hope to see Ruby help every programmer in the world be productive, and to enjoy programming, and to be happy.This is the primary purpose of Ruby language”
    9. 9. A primer
    10. 10. # booleanstruth = truelies = false# Stringsdude = Matzcredo = "#{dude} is nice, so we are nice!"# Numbers42 ** 1353.8 / 919 % 3
    11. 11. # Arraysbackpack = [6, apple, [true, some_object]]# Hasheshash = { true => He loves me!, dude => Must obey!, backpack => false}# symbolscities = [:paris, :new_york, :johannesburg]
    12. 12. Wtf is a symbol?
    13. 13. coders = [ { language => ruby, location => johannesburg }, { language => java, location => port elizabeth }, # ... { language => ruby, location => pretoria }] # vs.coders = [ { :language => ruby, :location => johannesburg }, { :language => java, :location => port elizabeth }, # ... { :language => ruby, :location => pretoria }]
    14. 14. Cool, what about classes?
    15. 15. public Class Person { private String firstName, lastName; public Person(String firstName, String lastName){ setFirstName(firstName); setLastName(lastName); } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public String setFirstName(String firstName){ this.firstName = firstName; } public String setLastName(String lastName){ this.lastName = lastName; }}
    16. 16. class Person attr_accessor :first_name, :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name endend
    17. 17. class Person attr_accessor :first_name, :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end endperson = Person.new( Nick, Cave)person.first_name = Slickperson.last_name = Naveputs "#{person.first_name} #{person.last_name}"
    18. 18. Btw, everything is an Object!
    19. 19. true.class() # => TrueClassfalse.to_s() # => "false"5.9.floor() # => 53.times{ puts(Hello JUG!) }# => Hello JUG!# => Hello JUG!# => Hello JUG!
    20. 20. module Earthend
    21. 21. module Earth class Person attr_reader :first_name, :last_name def initialize(first_name, last_name) @first_name, @last_name = first_name, last_name end endend
    22. 22. module Earth class Person attr_reader :first_name, :last_name def initialize(first_name, last_name) @first_name, @last_name = first_name, last_name end endend chap = Earth::Person.new(Chandler, Bing) chap.first_name # => Chandler
    23. 23. module RepublicPersonnel def storm_trooper Earth::Person.new(Storm, Trooper) endendclass CloningVat extend RepublicPersonnelend
    24. 24. module RepublicPersonnel def storm_trooper Earth::Person.new(Storm, Trooper) endendclass CloningVat extend RepublicPersonnelend#<Earth::Person @last_name="Trooper", @first_name="Storm">CloningVat.storm_trooper
    25. 25. module TehForce def sense ... a disturbance in the force endendmodule Earth class Person include TehForce endend
    26. 26. module TehForce def sense ... a disturbance in the force endendmodule Earth class Person include TehForce endend farm_boy = Earth::Person.new(Luke, Skywalker) # => ... a disturbance in the force farm_boy.sense
    27. 27. def some_days( something_happens ) puts The sun rises puts something_happens puts The sun setsenddef other_days( this_happens, and_that_happens) puts The sun rises puts this_happens puts and_that_happens puts The sun setsend some_days I do some work other_days I skip, I read a book
    28. 28. def some_days( something_happens ) puts The sun rises puts something_happens puts The sun setsend
    29. 29. def some_days( something_happens ) puts The sun rises puts something_happens puts The sun setsend
    30. 30. def some_days( something_happens ) puts The sun rises puts something_happens puts The sun setsenddef some_days puts The sun rises yield puts The sun setsendsome_days { puts I go to work }some_days do puts I skip puts I read a bookend
    31. 31. file = File.open(i <3 ruby.txt, w)file.puts "... i really do!"file.close
    32. 32. file = File.open(i <3 ruby.txt, w) file.puts "... i really do!" file.closeFile.open(i <3 ruby.txt, w) do |file| file.puts "... i really do!"end
    33. 33. Rubyists TATFT!describe Calculator do before(:each) do @calc = Calculator.new end context Addition do it returns 10 when adding 5 and 5 @calc.add(5,5).should == 10 end it returns 0 when adding 5 and -5 do @calc.add(5,-5).should == 0 end end # ...end
    34. 34. Metaprogramming
    35. 35. class Person def initialize @height, @weight, @hair_color = 6.2, 70kg, brown end %w(height weight hair).each do |property| define_method("#{property}") do i_var = self.instance_eval("@#{property}") i_var.to_s.upcase end end def method_missing(*args, &block) puts "I dont have #{args[0].to_s.gsub(/_/, )}" endendp = Person.newp.my_height # => 6.2p.my_weight # => 70KGp.my_hair_color # => BROWNp.your_car_keys # => I dont have your car keys
    36. 36. A primer
    37. 37. or....
    38. 38. Demo time!!
    39. 39. Interactive Ruby $ jirb jruby-1.6.7 :001 > require java => true jruby-1.6.7 :001 > #...
    40. 40. Ackermann
    41. 41. class Ackermann def self.ack(m, n) return n + 1 if m.zero? return ack(m - 1, 1) if n.zero? ack( m - 1, ack(m, n - 1) ) endend
    42. 42. include Rubeus::SwingJFrame.new(Ackerizer) do |frame| frame.layout = java.awt.FlowLayout.new @m, @n = JTextField.new(3),JTextField.new(10) JButton.new(->) do start = Time.now @result.text = Ackermann.ack( @m.text.to_i, @n.text.to_i ).to_s puts "#{Time.now - start}" end @result = JTextField.new 10 frame.pack frame.showend
    43. 43. class Ackermann def self.ack(m, n) return n + 1 if m.zero? return ack(m - 1, 1) if n.zero? ack( m - 1, ack(m, n - 1) ) endend
    44. 44. class Ackermann def self.ack(m, n) return n + 1 if m.zero? return ack(m - 1, 1) if n.zero? ack( m - 1, ack(m, n - 1) ) endendpublic class Ackermann { public static int ack(int m, int n) { if (m == 0) return n + 1; if (n == 0) return ack(m - 1, 1); return ack(m - 1, ack(m, n - 1)); }}
    45. 45. include Rubeus::SwingJFrame.new(Ackerizer) do |frame| frame.layout = java.awt.FlowLayout.new @m, @n = JTextField.new(3),JTextField.new(10) JButton.new(->) do start = Time.now @result.text = Java::Ackermann.ack( @m.text.to_i, @n.text.to_i ).to_s puts "#{Time.now - start}" end @result = JTextField.new 10 frame.pack frame.showend
    46. 46. Method overloadingpublic class OverloadTypeChecker { public static String inspect(long value) { return "long"; } public static String inspect(String value) { return "string"; } public static String inspect(Object value) { return "object"; }}
    47. 47. require javajava_import OverloadTypeChecker do OTCend# jRuby happily handles the simple caseOTC.inspect(5) # longOTC.inspect(Helloooo!) # stringOTC.inspect([]) # object
    48. 48. public class BitLength { public int neededFor(int i) { return 32; } public int neededFor(long l) { return 64; }}
    49. 49. # jRuby can be forced to use a specific overloadrequire javajava_import BitLengthbits = BitLength.newbits.needed_for(1_000_000) # 64
    50. 50. # jRuby can be forced to use a specific overloadrequire javajava_import BitLengthbits = BitLength.newbits.needed_for(1_000_000) # 64# java_sendbits.java_send :neededFor, [Java::int], 1_000_000 # 32
    51. 51. # jRuby can be forced to use a specific overloadrequire javajava_import BitLengthbits = BitLength.newbits.needed_for(1_000_000) # 64# java_sendbits.java_send :neededFor, [Java::int], 1_000_000 # 32# java_aliasclass BitLength java_alias :needed_for_int, :neededFor, [Java::int]endbits.needed_for_int 1_000_000 # 32
    52. 52. # jRuby can be forced to use a specific overloadrequire javajava_import BitLengthbits = BitLength.newbits.needed_for(1_000_000) # 64# java_sendbits.java_send :neededFor, [Java::int], 1_000_000 # 32# java_aliasclass BitLength java_alias :needed_for_int, :neededFor, [Java::int]endbits.needed_for_int 1_000_000 # 32# java_methodneeded = bits.java_method :neededFor, [Java::int]needed.call 1_000_000 # 32
    53. 53. ... and more about types
    54. 54. public class TypeChecker{ public static String check(Object o){ return o.getClass().getName(); }}require javaruby_array = [1, 2, Mushrooms, :sample]java_array = ruby_array.to_java# org.jruby.RubyArrayJava::TypeChecker.check( ruby_array )# [Ljava.lang.Object;Java::TypeChecker.check( java_array )# org.jruby.RubyArrayJava::TypeChecker.check( java_array.to_a )
    55. 55. Sooo, what about those C extensions... ?
    56. 56. Awesome, but why?• Cross platform GUI dev• Performance• Ruby interface to legacy code• Use a ruby test framework (Seriously, they’re amazing!)• Utilize java libraries• Sneak ruby into a java shop• Threading
    57. 57. import org.jruby.embed.InvokeFailedException;import org.jruby.embed.ScriptingContainer;public class RubyFromJava { public static void main(String[] args) { ScriptingContainer container = new ScriptingContainer(); container.runScriptlet("puts Java touched my Ruby"); }} $ java -cp jruby-complete.jar:. RubyFromJava Java touched my Ruby
    58. 58. More Demos!!
    59. 59. http://pragprog.com/book/jruby/using-jruby
    60. 60. http://www.meetup.com/Code-Coffee-JHB/
    61. 61. http://www.meetup.com/RubyOnBeer/
    62. 62. Questions?
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×