Quick Intro To JRuby
Upcoming SlideShare
Loading in...5
×
 

Quick Intro To JRuby

on

  • 4,596 views

A shorter version of my Introduction to JRuby talk that was given at the Colorado Springs Open Source User Group.

A shorter version of my Introduction to JRuby talk that was given at the Colorado Springs Open Source User Group.

Statistics

Views

Total Views
4,596
Slideshare-icon Views on SlideShare
4,248
Embed Views
348

Actions

Likes
6
Downloads
47
Comments
0

9 Embeds 348

http://fredjean.net 284
http://localhost 27
http://fredjean.net.s3-website-us-east-1.amazonaws.com 12
http://www.slideshare.net 11
http://blog.fredjean.net 7
http://posterous.com 4
http://feeds.feedburner.com 1
http://coderwall.com 1
https://www.linkedin.com 1
More...

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

    Quick Intro To JRuby Quick Intro To JRuby Presentation Transcript

    • A (Quick) Introduction to JRuby Frederic Jean fred@fredjean.net
    • Hello JRuby
    • Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries
    • Ruby
    • Ruby is... • Dynamically Typed • Strongly Typed • Everything is An Object
    • (Almost) Everything is an Object
    • 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
    • Symbols :symbol • String-like construct • Only one copy in memory
    • Blocks ['John', 'Henry', 'Mark'].each { |name| puts quot;#{name}quot;} File.open(quot;/tmp/password.txtquot;) do |input| text = input.read end
    • Ruby Classes class Person attr_accessor :name, :title end p = Person.new
    • Open Classes class Fixnum def odd? self % 2 == 1 end def even? self % 2 == 0 end end http://localhost:4567/code/fixnum.rb
    • Mixins module Parity def even? self % 2 == 0 end def odd? self % 2 == 1 end end http://localhost:4567/code/parity.rb
    • 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) if self.last_name == other.last_name self.first_name <=> other.first_name else self.last_name <=> other.last_name end end def to_s quot;#{first_name} #{last_name}quot; end end http://localhost:4567/code/sorting.rb
    • JRuby
    • JRuby • Ruby Implementation on the JVM • Compatible with Ruby 1.8.6 • Native Multithreading • Full Access to Java Libraries
    • JRuby Hello World java.lang.System.out.println quot;Hello JRuby!quot; http://localhost:4567/code/jruby_hello.rb
    • Java Integration require 'java'
    • 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?
    • 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
    • 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
    • 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
    • JI: Interface Conversion package java.util.concurrent; public class Executors { // ... public static Callable callable(Runnable r) { // ... } }
    • 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
    • JI: Closure Conversion callable = Executors.callable { puts quot;hiquot; } callable.call http://localhost:4567/code/ji/closure_conversion.rb
    • Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries
    • Usage • Runtime • Wrapper around Java Code • Calling From Java • Utilities
    • JRuby as a Runtime
    • JRuby on Rails • Wrap Rails app into a war file • Direct support in Glassfish v3 Prelude • http://kenai.com • http://mediacast.sun.com
    • 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
    • Wrapping Java Code
    • Swing Applications frame = JFrame.new(quot;Hello Swingquot;) • Reduce verbosity • Simplify event handlers http://localhost:4567/code/swing.rb
    • 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
    • Utilities
    • Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries
    • Build Utilities • Rake • Raven • Buildr
    • Conclusion
    • Resources • http://jruby.org • http://wiki.jruby.org • http://jruby.kenai.com • http://jtester.codehaus.org • http://raven.rubyforge.org • http://buildr.apache.org