Download presentation
Presentation is loading. Please wait.
Published byDylan Reed Modified over 8 years ago
1
JRuby: Ruby and Rails for the JVM Charles Nutter Except where otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License (http://creativecommons.org/licenses/by-sa/3.0/us/).
2
2 Your Humble Presenter Charles Oliver Nutter Longtime Java developer (10+ yrs) Shorttime engineer at Sun Microsystems Full-time JRuby developer Also working to build out JVM dynlang support Wide range of past experience C, C++, C#, Python, Delphi, Lisp, Scheme Java EE and ME
3
3 Off We Go! Ruby 101
4
4 What Is Ruby Dynamic-typed, pure OO language Interpreted Open source, written in C Good: easy to write, easy to read, powerful, “fun” Bad: green threads, unicode support, libraries, “slow” Created 1993 by Yukihiro “Matz” Matsumoto “More powerful than Perl and more OO than Python” Very active community, wide range of apps
5
5 Why Ruby? Easy to learn Extremely powerful Potentially the “most” dynamic of dynlangs Large and growing set of libs and apps Large and growing community “Fun” factor can't be understated Fast growing popularity
6
6 TIOBE Programming Community Index
7
7 Pure OO Everything is an Object Circle.new(4)=> instance of Circle “abc”.length => 3 1.to_s => “1” All Objects are instances of Classes 1.class=> Fixnum Single-Inheritance Object is base class of all classes
8
8 Literals Fixnum: 1 Float: 1.0 Bignum: 12345678987654321 String: “one” 'one' %Q[one] %q[one]... Multiline string (“here doc”): x = <<EOS extend across two lines EOS Regexp: /^foo\w+.*bar$/, %r[^foo$] Array: [1, “ein”, :ichi] Hash: {:one => 1, :two => 2}
9
9 Basics String substitution a = “foo” b = “bar#{a}baz” => “barfoobaz” Operator overloading def +(arg);... Attributes class Foo attr_accessor :a end x = Foo.new x.a = “hello” puts x.a => “hello”
10
10 Dynamically Typed Variables do not declare type a = 1 a = “one” a = [1, “ein”, a] Strongly typed: objects stay the same type...but type is less important than supported ops Don't rely on incoming types Rely on expected operations Often called “Duck Typing” today
11
11 Duck Typing “If it waddles like a duck and it quacks like a duck...” Runtime errors seem scary, but rarely happen Good unit testing helps prevent those rare cases def make_it_waddle(waddler) waddler.waddle end make_it_waddle(Duck.new) make_it_waddle(Penguin.new) make_it_waddle(Octopus.new)
12
12 A Simple Class class Circle < Object def initialize(radius) @radius = radius end... def area() return Math::PI * @radius ** 2 end Circle.new(4)
13
13 Circle extends Object class Circle < Object def initialize(radius) @radius = radius end... def area() return Math::PI * @radius ** 2 end Circle.new(4)
14
14 Constructors are named “initialize” class Circle < Object def initialize(radius) @radius = radius end... def area() return Math::PI * @radius ** 2 end Circle.new(4)
15
15 Instance variables prefixed with @ class Circle < Object def initialize(radius) @radius = radius end... def area() return Math::PI * @radius ** 2 end Circle.new(4)
16
16 Modules: Mixin inheritance module ShapeStuff def diameter return 2 * @radius end class Circle include ShapeStuff... end Circle.new(4).diameter => 8
17
17 Blocks or Closures # Two syntaxes: Curly brackets {} or do... end [1, 2, 3].each {|number| puts “I see #{number}” } [1, 2, 3].each do |number| puts “I see #{number}” end def foo yield “hello” end def foo2(&block) block.call(“hello”) end
18
18 Why Blocks? Removes repetitive code No need for the same old external iterator code list.each { |element|... do something... } Transactional logic can be internalized File.open(fname) {|file| line = file.gets... }
19
19 Metaprogramming Create and modify classes Add, alias, remove, or replace methods Include a module at any time Add instance or class variables at any time Special hooks to lazily metaprogram send – call methods by name method_missing – for when methods don't exist const_missing – for when constants aren't defined
20
20 NetBeans Ruby Support Best Ruby IDE available code completion in dozens of ways smart syntax highlighting more and more refactorings jump to method, variable, class declarations pop-up RDoc full project support for Ruby or Rails apps everything else IDEs do: version control, etc
21
21 Demo Ruby Programming in NetBeans
22
22 What is JRuby? Started in 2002 Java implementation of Ruby language Open source, many active contributors Compatible with current Ruby version Easy integration with Java libraries, infrastructure Call to Ruby from Java via JSR223, BSF Use Java classes from Ruby (e.g. Script Java) Growing set of external projects based on JRuby
23
23 Getting JRuby Download from JRuby.org
24
24 Getting JRuby Build it yourself
25
25 Demo Interactive Java
26
26 Why JRuby? JVM is an excellent platform Vast library of quality Java libraries Wide distribution Faster every day; microbenches faster now Potential for much better unicode, threading Deploy to standard app servers
27
27 Java: The Everytool
28
28 Java: The Everytool
29
29 Demo Profligacy: The Swing Reducer ihate.rubyforge.org/profligacy/ prof·li·gate: adj. 1. Given over to dissipation; dissolute. 2. Recklessly wasteful; wildly extravagant.
30
30 Demo Cheri: A Builder Framework for (J)Ruby and Java cheri.rubyforge.org
31
31 Now It Gets Interesting JRuby on Rails: The Basics
32
32 What is Rails Full Stack Framework ActiveSupport provides base frameworks ActiveRecord for database access (model) ActionPack for controllers and views ActionWebService, ActionMailer Rapid Development Code generators for models, views, controllers, unit tests, plugins, ajax interfaces, web services, and more Schema management/versioning DSL (“migrations”)
33
33 Why Rails Terse and Elegant “One-tenth as much code as Java” Convention over Configuration Don't Repeat Yourself A “DSL for web development” Agile A running application from day one A lightweight development server Incremental, test-driven development
34
34 Why JRuby on Rails All the Ruby reasons are important for webapps Improved deployment story with Java app servers Native Unicode a must for i18n web dev Broader, more scalable database support Integration with Java libs, legacy services Easier to switch web framework than app arch
35
35 Demo Creating a Simple Rails Application
36
36 How Easy Can It Be? Rails Deployment
37
37 Classic Option #1: Just use Mongrel
38
38 Classic Mongrel is supported by JRuby Some bits won't work Forking off subprocesses Process management Some folks are running JRuby/Mongrel Most typical Mongrel setups should work Mongrel JCluster is recommended
39
39 Normal RoR With Mongrel
40
40 JRoR with Mongrel JCluster
41
41 The New Way Option #2: Deploy to an app server
42
42 East Meets West GoldSpike: Rails plugin for building WAR files One plugin, pure Ruby Out comes a deployable WAR file Server-agnostic But GlassFish fairly rocks (glassfish.dev.java.net)
43
43 JRoR with GoldSpike
44
44 Demo Using GoldSpike with GlassFish
45
45 Middle Ground Option #3: GlassFish v3 Rails Support
46
46 Best of Both Worlds? ~/work/jruby $ gem install glassfish-rails ~/work/jruby $ glassfish_rails ~/myapp => Starting GlassFish => Rails application at http://localhost:8080 => Admin services at http://localhost:4848 => Clustering enabled => Connection pooling enabled => Load balancing enabled => Server Ready.
47
47 JRoR with GoldSpike
48
48 Demo Glassfish v3 Rails Preview
49
49 The Transformed Man Beyond Rails Creating your own Chimera
50
50 Chimera Chimera, noun 1: (Greek mythology) fire-breathing she-monster with a lion's head and a goat's body and a serpent's tail; daughter of Typhon [syn: Chimera, Chimaera] 2: a grotesque product of the imagination [syn: chimaera]
51
51 Chimera Chimera, noun 1: (Greek mythology) fire-breathing she-monster with a lion's head and a goat's body and a serpent's tail; daughter of Typhon [syn: Chimera, Chimaera] 2: a grotesque product of the imagination [syn: chimaera] 3: (JRuby mythology) fire-breathing program with a Rails head and a Java body and/or tail.
52
52 There's a Whole Platform Out There Billions of lines of Java code out there Tens of thousands of deployments Thousands of libraries The Java platform is mature, trusted, reliable...and often boring! So why not combine two greats? Ruby as the programming language Java for the platform and libraries
53
53 Java EE 5: The Choicest Cuts
54
54 Our Chimera Combining features Ruby or Rails as the application layer Java libraries, alone or as ported gems Java-based services...legacy integration The JVM Acceptable to today's enterprise “Enterprise-ready” without losing the Ruby Way To them: “it's just Java”, to you: “It's just Ruby”
55
55 Demo ActiveHibernate code.google.com/p/activehibernate/
56
56 Other Projects to Watch ActiveMessaging JMS support Spring on Rails Rake/Rant/Buildr/Raven ActiveJPA?
57
57 The Future of JRuby JRuby 1.0.1 to be released soon JRuby 1.1 by November Full compilation to bytecode Reworked IO library Intensive concurrency testing Enhancing Java integration Compile to static classes? Extend Ruby classes with Java subclasses?
58
58 A New Beginning JRuby: www.jruby.org JRuby Wiki: www.headius.com/jrubywiki Ruby:www.ruby- lang.org Rails: www.rubyonrails.org NetBeans: www.netbeans.org NetBeans Ruby: wiki.netbeans.org/wiki/view/Ruby Blog: headius.blogspot.com
59
59 Credits “The ultimate Swiss Army Knife for sale in Interlaken” by redjar, http://www.flickr.com/photos/redjar/113974357/, http://creativecommons.org/licenses/by-sa/2.0/ http://www.flickr.com/photos/redjar/113974357/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.