Intro to J Ruby

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Intro to J Ruby - Presentation Transcript

    1. Introduction to JRuby Frederic Jean fred@fredjean.net 1
    2. First Encounter 2
    3. Hello JRuby 3
    4. Configuration DSLs Glue, Wiring Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries http://olabini.com/blog/2008/01/language-explorations/ 4
    5. Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries 5
    6. Ruby 6
    7. Ruby is... • Dynamically Typed • Strongly Typed • Everything is An Object 7
    8. Dynamic Typing • Variables Are Not Typed • Type Evaluated at Run Time • Duck Typing 8
    9. (Almost) Everything is an Object 9
    10. Ruby Literals # Strings \"This is a String\" 'This is also a String' %{So is this} # Numbers 1, 0xa2, 0644, 3.14, 2.2e10 # Arrays and Hashes ['John', 'Henry', 'Mark'] { :hello => \"bonjour\", :bye => \"au revoir\"} # Regular Expressions \"Mary had a little lamb\" =~ /little/ \"The London Bridge\" =~ %r{london}i # Ranges (1..100) # inclusive (1...100) # exclusive 10
    11. Symbols :symbol • String-like construct • Only one copy in memory 11
    12. Ruby Type \"hello\".class # => String 1.class # => Fixnum 10.3.class # => Float [].class # => Array {}.class # => Hash Kernel.class # => Module Object.class # => Class :symbol.class # => Symbol 12
    13. 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
    14. Blocks ['John', 'Henry', 'Mark'].each { |name| puts \"#{name}\"} File.open(\"/tmp/password.txt\") do |input| text = input.read end 14
    15. Ruby Classes class Person attr_accessor :name, :title end p = Person.new 15
    16. Open Classes class Fixnum def odd? self % 2 == 1 end def even? self % 2 == 0 end end http://localhost:4567/code/fixnum.rb 16
    17. Namespacing module Net module FredJean class MyClass end end end Net::FredJean::MyClass 17
    18. Mixins module Parity def even? self % 2 == 0 end def odd? self % 2 == 1 end end http://localhost:4567/code/parity.rb 18
    19. 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 \"#{first_name} #{last_name}\" end end http://localhost:4567/code/sorting.rb 19
    20. Mixin Reflection puts String.include? Enumerable puts String.instance_of? Enumerable puts String.kind_of? Enumerable http://localhost:4567/code/include.rb 20
    21. Executable Class Definition class Message if RUBY_PLATFORM =~ /java/i def hello puts \"Hello From JRuby\" end end end http://localhost:4567/code/env.rb 21
    22. JRuby 22
    23. JRuby • Ruby Implementation on the JVM • Compatible with Ruby 1.8.6 • Native Multithreading • Full Access to Java Libraries 23
    24. JRuby Hello World java.lang.System.out.println \"Hello JRuby!\" http://localhost:4567/code/jruby_hello.rb 24
    25. Java Integration require 'java' 25
    26. 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
    27. JI: Java Extensions h = java.util.HashMap.new h[\"key\"] = \"value\" h[\"key\"] # => \"value\" h.get(\"key\") # => \"value\" h.each {|k,v| puts k + ' => ' + v} # key => value h.to_a # => [[\"key\", \"value\"]] http://localhost:4567/code/ji/extensions.rb 27
    28. 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
    29. 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
    30. JI: Interface Conversion package java.util.concurrent; public class Executors { // ... public static Callable callable(Runnable r) { // ... } } 30
    31. JI: Interface Conversion class SimpleRubyObject def run puts \"hi\" end end callable = Executors.callable(SimpleRubyObject.new) callable.call http://localhost:4567/code/ji/if_conversion.rb 31
    32. JI: Closure Conversion callable = Executors.callable { puts \"hi\" } callable.call http://localhost:4567/code/ji/closure_conversion.rb 32
    33. Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries 33
    34. Usage • Runtime • Wrapper around Java Code • Calling From Java • Utilities 34
    35. JRuby as a Runtime 35
    36. 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
    37. Warbler jruby -S gem install warbler jruby -S warble config # => config/warble.rb jruby -S warble # => myapp.war 37
    38. 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 \"txmt://open/?url=file://#{local_file}&line=#{line}\" else not_found end end http://localhost:4567/code/webmate.rb 38
    39. Wrapping Java Code 39
    40. Swing Applications frame = JFrame.new(\"Hello Swing\") • Reduce verbosity • Simplify event handlers http://localhost:4567/code/swing.rb 40
    41. RSpec Testing Java describe \"An empty\", HashMap do before :each do @hash_map = HashMap.new end it \"should be able to add an entry to it\" do @hash_map.put \"foo\", \"bar\" @hash_map.get(\"foo\").should == \"bar\" end end JTestr Provides Ant and Maven integration http://jtestr.codehaus.org http://localhost:4567/code/hashmap_spec.rb 41
    42. Java Calling JRuby 42
    43. JSR-223 • Ships with Java 6 • Supports JRuby 1.1 https://scripting.dev.java.net/ 43
    44. JRuby Runtime • Support the current release • Specific to JRuby • Subject to change 44
    45. Spring Scripting <lang:jruby id=\"lime\" script-interfaces=\"springruby.Lime\" script-source=\"classpath:lime.rb\"/> 45
    46. Utilities 46
    47. Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries 47
    48. Build Utilities • Rake • Raven • Buildr 48
    49. Conclusion 49
    50. 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
    SlideShare Zeitgeist 2009

    + fredjeanfredjean Nominate

    custom

    243 views, 0 favs, 0 embeds more stats

    Slides for my Introduction to JRuby talk.

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 243
      • 243 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 3
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?