Scripting Java with JRuby

Slides:



Advertisements
Similar presentations
In Review JAVA C++ GUIs - Windows Webopedia.com.
Advertisements

Developing in CAS. Why? As distributed you edit CAS 3 with Eclipse and build with Maven 2 – Best Practice for Release Engineering – Difficult edit-debug.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
The Ruby Programming Language
+ Java vs. Javascript Jessi Style. + Java Compiled Can stand on its own Written once, run anywhere Two-stage debugging Java is an Object Oriented Programming.
4.1 JavaScript Introduction
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
JRuby: Bringing Ruby to the JVM™ Thomas E. Enebo, Aandtech Inc. Charles Oliver Nutter, Ventera Corp
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
.NET Framework Danish Sami UG Lead.NetFoundry
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
COP 3330 Notes 1/12. Today's topics Downloading Java and Eclipse Hello World Basic control structures Basic I/O Strings.
1 Web Based Programming Section 8 James King 12 August 2003.
Ruby on Rails WTF? Created by Matz in 1993 Inspired by Smalltalk Rubygems = CSPAN Rake = ant Rdoc = javadoc.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
COMP3019 Coursework Help Steve Crouch School of Electronics and Computer Science.
JavaScript Syntax, how to use it in a HTML document
Templating, Routing, lodash Extending functionality using Collections SoftUni Team Technical Trainers Software University
Applied Computing Technology Laboratory QuickStart Java Learning to Program in Java Dr. Tom Way October 21, 2005.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
RUBY by Ryan Chase.
M1G Introduction to Programming 2 5. Completing the program.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
© 2012 LogiGear Corporation. All Rights Reserved FitNesseFitNesse Authors: Nghia Pham 1.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA TRAINING IN NOIDA. JAVA Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented and specifically.
Presented By P.SRIVIDYA 085D1A0552 Programming Language.
1 JRuby: Up and Running! Charles Oliver Nutter and Thomas Enebo The JRuby Guys Sun Microsystems Except where otherwise noted, the content of this presentation.
NetBeans & Ruby David Botterill Software Engineer Global Technology Manager - Campus Ambassador Program Sun Microsystems, Inc.
1 JRuby Charles Oliver Nutter JRuby Guy Sun Microsystems 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.
1 Charles Oliver Nutter Thomas Enebo Tor Norbye Martin Krauskopf JRuby: Understanding the Fuss Except where otherwise noted, the content of this presentation.
Object Oriented Programming in
Java Interview Questions
Programming Languages Dan Grossman 2013
Ruby Getting started & Ruby.New Presented by Mario Tayah
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
Scope History of Ruby. Where can you use Ruby? General Features.
JRuby: Up and Running! Charles Oliver Nutter and Thomas Enebo
University of Central Florida COP 3330 Object Oriented Programming
JRuby Charles Oliver Nutter
JRuby on Rails Charles Oliver Nutter JRuby Core Developer
The plan What's JRuby? The Ruby language Why JRuby? How to use it
Longtime Java developer More recent Ruby developer
JRuby It's What's For Dinner
Introduction Enosis Learning.
Ruby, Rails, GUIs, and More
JRuby State of the Art! Except where otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution-Share Alike 3.0.
JRuby Charles Oliver Nutter JRuby Guy Sun Microsystems
JRuby: Up and Running! Charles Oliver Nutter and Thomas Enebo
Module 1: Getting Started
Introduction Enosis Learning.
JavaScript an introduction.
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
Advanced Programming Fall 2017.
JRuby: Bringing Ruby to the JVM™
Java Applets.
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
Introducing Java.
Code Blocks, Closures, and Continuations
Presentation transcript:

Scripting Java with JRuby Charles Oliver Nutter Thomas Enebo Sun Microsystems, Inc www.jruby.org 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/).

Which language can do all these: Call Java libraries from scripted code Decorate Java classes with new methods Implement Java interfaces Extend Java classes Support annotations and reflection Deploy on Java app servers Run in applets Run on mobile devices Run on Google AppEngine for Java

JRuby!

JRuby: Ruby for the JVM Ruby is: Dynamically-typed Object-oriented Rich syntax Vibrant community www.ruby-lang.org Lots of fun! JRuby is: High performance Real native threads Access to Java libs Run on Java servers Embed as a JAR www.jruby.org Lots of fun!

Getting Started Download JRuby http://tinyurl.com/getjruby jruby-bin-1.3.0.{tar.gz,zip} Unpack JRuby Run 'jruby' or 'jruby.bat' from bin dir Put in your PATH env var if you like

Getting Started

Ruby Tour: Object Oriented 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

Ruby Tour: Literal Values Numbers: 1, 1.0, 12345678987654321 String: “one” 'one' %Q[one] %q[one] ... Multiline string (“here doc”): x = <<EOS extend across two lines EOS Symbol: :one, %s[one] Regexp: /^foo\w+.*bar$/, %r[^foo$] Array: [1, “ein”, :ichi] Hash: {:one => 1, :two => 2}

Ruby Tour: Duck Typing Dynamic typing “If it waddles like a duck and it quacks like a duck...” def make_it_waddle(animal) animal.waddle end make_it_waddle(Duck.new) make_it_waddle(Penguin.new) make_it_waddle(Octopus.new)

Ruby Tour: Class Definition class Car < Object # '< Object' optional # initialize is Ruby's constructor def initialize(color) @color = color end def color @color # construct a Hello object my_car = Car.new("Blue") puts my_car.color => "Blue"

Ruby Tour: Attributes class Car attr_reader :color # initialize is Ruby's constructor def initialize(color) @color = color end # construct a Hello object my_car = Car.new("Blue") puts my_car.color => "Blue"

Ruby Tour: Blocks (Closures) # two formats: braces {} and do..end File.open('my_file') do |open_file| line = open_file.gets ... end # methods that accept blocks def File.open(filename) open_file = open(filename) begin yield open_file ensure open_file.close

Ruby Tour: Modules # A collection of objects class MyProducts # Enumerable provides iteration methods include Enumerable # define an 'each' method that iterates def each # yield to each element in turn end list = MyProducts.new list.select {|item| item.price > 5.00} list.sort {|a, b| a.name <=> b.name} list.max

Getting Started Scripting Java Require the 'java' library In code: require 'java' Using a flag: -r java Import classes, interfaces, etc Script them!

Bonuses! Jar files are libraries you can require require 'itext.jar' Add to $CLASSPATH at runtime $CLASSPATH << 'itext.jar' Java methods in camel or ruby case currentTimeMillis or current_time_millis Bean-like properties are attributes frame.always_on_top = true

Call Java Libraries import java.util.ArrayList list = ArrayList.new 5.times {|i| list.add I} iter = list.iterator puts iter.next while iter.has_next? A Ruby block or closure

Call Java Libraries import java.util.ArrayList list = ArrayList.new 5.times {|i| list.add i} iter = list.iterator puts iter.next while iter.has_next?

Decorate Java classes import java.lang.System import java.util.ArrayList list = ArrayList.new 5.times {|i| list << I} list.each {|element| puts element } Using Ruby's << operator to append to a Java collection! Using Ruby's “each” method on a Java collection!

Decorate Java classes import java.lang.System import java.util.ArrayList list = ArrayList.new 5.times {|i| list << I} list.each {|element| puts element } Using Ruby's << operator to append to a Java collection! Using Ruby's “each” method on a Java collection!

Implement Interfaces JThread = java.lang.Thread JThread.new { 5.times { puts 'hello' } }.start import java.lang.Runnable class MyTask include Runnable def run; 5.times { puts 'goodbye' }; end end JThread.new(MyTask.new).start Using a Ruby closure/block to implement a Java interface! Include a Java interface to implement it (good for larger interfaces)

Implement Interfaces JThread = java.lang.Thread JThread.new { 5.times { puts 'hello' } }.start import java.lang.Runnable class MyTask include Runnable def run; 5.times { puts 'goodbye' }; end end JThread.new(MyTask.new).start Using a Ruby closure/block to implement a Java interface! Include a Java interface to implement it (good for larger interfaces)

Extend Classes import java.util.HashMap class LoggingMap < HashMap def put(a, b) puts “adding key: #{a} value: #{b}” super end my_map = LoggingMap.new my_map.put(1, 'hello'); puts my_map

Extend Classes import java.util.HashMap class LoggingMap < HashMap def put(a, b) puts “adding key: #{a} value: #{b}” super end my_map = LoggingMap.new my_map.put(1, 'hello'); puts my_map

Annotations and Reflection Ruby2Java gem Input: Ruby source with signatures, annotations Output: Java class Normal Java class Import into Java code Construct with “new” Call or reflect methods Coming soon; prototype works now

Deploy to Java Servers ~/my_rails_app $ warble …copying files mkdir -p tmp/war/WEB-INF jar cf my_rails_app.war -C tmp/war . ~/my_rails_app $ asadmin deploy my_rails_app.war

Deploy to Java Servers Use Warbler to package as WAR file JRuby + framework + deps, all in one Deploy on any web container Simple to use gem install “warbler” Run “warble” inside application dir Deploy resulting WAR file

Applets and Mobile Compiled or interpreted: fewer security hassles Include all or none of Ruby libs for size Check JRuby.org soon for an applet demo JRuby community working on mobile apps

Scripting Java with JRuby DEMO DEMO DEMO

Learn More! Visit www.jruby.org Email us Charlie: charles.nutter@sun.com Tom: thomas.enebo@sun.com Find us on IRC #jruby on freenode.net Twitter @headius and @tom_enebo