• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Intro to J Ruby
 

Intro to J Ruby

on

  • 1,309 views

Slides for my Introduction to JRuby talk.

Slides for my Introduction to JRuby talk.

Statistics

Views

Total Views
1,309
Views on SlideShare
1,308
Embed Views
1

Actions

Likes
0
Downloads
18
Comments
0

1 Embed 1

http://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Intro to J Ruby Intro to J Ruby Presentation Transcript

    • Introduction to JRuby Frederic Jean fred@fredjean.net 1
    • First Encounter 2
    • Hello JRuby 3
    • Configuration DSLs Glue, Wiring Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries http://olabini.com/blog/2008/01/language-explorations/ 4
    • Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries 5
    • Ruby 6
    • Ruby is... • Dynamically Typed • Strongly Typed • Everything is An Object 7
    • Dynamic Typing • Variables Are Not Typed • Type Evaluated at Run Time • Duck Typing 8
    • (Almost) Everything is an Object 9
    • Ruby Literals # Strings quot;This is a Stringquot; 'This is also a String' %{So is this} # Numbers 1, 0xa2, 0644, 3.14, 2.2e10 # Arrays and Hashes ['John', 'Henry', 'Mark'] { :hello => quot;bonjourquot;, :bye => quot;au revoirquot;} # Regular Expressions quot;Mary had a little lambquot; =~ /little/ quot;The London Bridgequot; =~ %r{london}i # Ranges (1..100) # inclusive (1...100) # exclusive 10
    • Symbols :symbol • String-like construct • Only one copy in memory 11
    • Ruby Type quot;helloquot;.class # => String 1.class # => Fixnum 10.3.class # => Float [].class # => Array {}.class # => Hash Kernel.class # => Module Object.class # => Class :symbol.class # => Symbol 12
    • kind_of? vs respond_to? puts 10.instance_of? Fixnum puts 10.respond_to? :odd? puts 10.respond_to? :between? http://localhost:4567/code/instance_of.rb 13
    • Blocks ['John', 'Henry', 'Mark'].each { |name| puts quot;#{name}quot;} File.open(quot;/tmp/password.txtquot;) do |input| text = input.read end 14
    • Ruby Classes class Person attr_accessor :name, :title end p = Person.new 15
    • Open Classes class Fixnum def odd? self % 2 == 1 end def even? self % 2 == 0 end end http://localhost:4567/code/fixnum.rb 16
    • Namespacing module Net module FredJean class MyClass end end end Net::FredJean::MyClass 17
    • Mixins module Parity def even? self % 2 == 0 end def odd? self % 2 == 1 end end http://localhost:4567/code/parity.rb 18
    • Mixins class Person include Enumerable attr_accessor :first_name, :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end def <=>(other) self.last_name <=> other.last_name && self.first_name <=> other.last_name end def to_s quot;#{first_name} #{last_name}quot; end end http://localhost:4567/code/sorting.rb 19
    • Mixin Reflection puts String.include? Enumerable puts String.instance_of? Enumerable puts String.kind_of? Enumerable http://localhost:4567/code/include.rb 20
    • Executable Class Definition class Message if RUBY_PLATFORM =~ /java/i def hello puts quot;Hello From JRubyquot; end end end http://localhost:4567/code/env.rb 21
    • JRuby 22
    • JRuby • Ruby Implementation on the JVM • Compatible with Ruby 1.8.6 • Native Multithreading • Full Access to Java Libraries 23
    • JRuby Hello World java.lang.System.out.println quot;Hello JRuby!quot; http://localhost:4567/code/jruby_hello.rb 24
    • Java Integration require 'java' 25
    • JI: Ruby-like Methods Java Ruby Locale.US Locale::US System.currentTimeMillis() System.current_time_millis locale.getLanguage() locale.language date.getTime() date.time date.setTime(10) date.time = 10 file.isDirectory() file.directory? 26
    • JI: Java Extensions h = java.util.HashMap.new h[quot;keyquot;] = quot;valuequot; h[quot;keyquot;] # => quot;valuequot; h.get(quot;keyquot;) # => quot;valuequot; h.each {|k,v| puts k + ' => ' + v} # key => value h.to_a # => [[quot;keyquot;, quot;valuequot;]] http://localhost:4567/code/ji/extensions.rb 27
    • JI: Java Extensions module java::util::Map include Enumerable def each(&block) entrySet.each { |pair| block.call([pair.key, pair.value]) } end def [](key) get(key) end def []=(key,val) put(key,val) val end end 28
    • JI: Open Java Classes class Java::JavaLang::Integer def even? int_value % 2 == 0 end def odd? int_value % 2 == 1 end end http://localhost:4567/code/ji/integer.rb 29
    • JI: Interface Conversion package java.util.concurrent; public class Executors { // ... public static Callable callable(Runnable r) { // ... } } 30
    • JI: Interface Conversion class SimpleRubyObject def run puts quot;hiquot; end end callable = Executors.callable(SimpleRubyObject.new) callable.call http://localhost:4567/code/ji/if_conversion.rb 31
    • JI: Closure Conversion callable = Executors.callable { puts quot;hiquot; } callable.call http://localhost:4567/code/ji/closure_conversion.rb 32
    • Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries 33
    • Usage • Runtime • Wrapper around Java Code • Calling From Java • Utilities 34
    • JRuby as a Runtime 35
    • JRuby on Rails • Ruby on Rails 2.1 and above is supported • Direct support in Glassfish v3 Prelude • http://kenai.com • http://mediacast.sun.com 36
    • Warbler jruby -S gem install warbler jruby -S warble config # => config/warble.rb jruby -S warble # => myapp.war 37
    • webmate require 'rubygems' require 'sinatra' get '/*' do file, line = request.path_info.split(/:/) local_file = File.join(Dir.pwd, file) if (File.exists?(local_file)) redirect quot;txmt://open/?url=file://#{local_file}&line=#{line}quot; else not_found end end http://localhost:4567/code/webmate.rb 38
    • Wrapping Java Code 39
    • Swing Applications frame = JFrame.new(quot;Hello Swingquot;) • Reduce verbosity • Simplify event handlers http://localhost:4567/code/swing.rb 40
    • RSpec Testing Java describe quot;An emptyquot;, HashMap do before :each do @hash_map = HashMap.new end it quot;should be able to add an entry to itquot; do @hash_map.put quot;fooquot;, quot;barquot; @hash_map.get(quot;fooquot;).should == quot;barquot; end end JTestr Provides Ant and Maven integration http://jtestr.codehaus.org http://localhost:4567/code/hashmap_spec.rb 41
    • Java Calling JRuby 42
    • JSR-223 • Ships with Java 6 • Supports JRuby 1.1 https://scripting.dev.java.net/ 43
    • JRuby Runtime • Support the current release • Specific to JRuby • Subject to change 44
    • Spring Scripting <lang:jruby id=quot;limequot; script-interfaces=quot;springruby.Limequot; script-source=quot;classpath:lime.rbquot;/> 45
    • Utilities 46
    • Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries 47
    • Build Utilities • Rake • Raven • Buildr 48
    • Conclusion 49
    • Resources • http://jruby.org • http://wiki.jruby.org • http://jruby.kenai.com • http://jtester.codehaus.org • http://raven.rubyforge.org • http://buildr.apache.org 50