34
Singing Java with JRuby Sinatra K.Gautam @gautamNitish A melodious song on JRuby

JRuby and Sinatra

Embed Size (px)

Citation preview

Singing Java with JRuby Sinatra

K.Gautam@gautamNitish

A melodious song on JRuby

Ruby the language

Created by Yukihiro Matsumoto

JRubyDevelopers: Ola Bini,Nick Sieger,Charles Nutter,Thomas Enebo.

Blake Mizerany and Many other Conrtibuters

Sinatra

So Lets look at Ruby

Hard Facts on

Dynamic

Hard Facts on

Interpreted

Hard Facts on

Reflective

Hard Facts on

ObjectOriented

Hard Facts on Im

plemented in C

So what is JRuby ?

+ =Isn't that obvious from the name ?

So JRuby is just another Ruby interpreter.

Why JRuby?

Why JRuby?

True Multi-threading

Why JRuby?

A well tuned Virtual Machine

Why JRuby?

Simple and Easy Language

Why JRuby?

Ruby was designed to make programmers Happy

Why JRuby?

Fewer Bugs with Ruby

So Lets compare Java and Ruby ....

And Why its better to combine them

Java vs Ruby

Java

Ruby

1 class Foo{2 public static void main (String []args){3 }4 }

The Minimum Program

Java vs Ruby

Java

Ruby

1 class Foo{ 2 private int bar1 , bar2; 3 public int getBar1(){ return this.bar1; } 4 public int getBar2(){ return this.bar2; } 5 public void setBar2(int bar2){ 6 this.bar2=bar2; 7 } 8 public void setBar1(int bar1){ 9 this.bar1=bar1;10 }11 }

1 class Foo2 attr_accessor :bar1 , :bar23 end

Getters and Setters

Java vs Ruby

Java

Ruby

1 List<String> friends = new LinkedList<String>();2 List<String> enemies = new LinkedList<String>();3 friends.add("Java"); friends.add("JRuby");4 friends.add("Jython"); friends.add("Scala")5 enemies.add("C#"); enemies.add("IronRuby");6 enemies.add("IronPython"); enemies.add("F#");

1 friends=[]2 enemies=[]3 friends<< "Java" << "JRuby" << "Jython" << "Scala"4 enemies<< "c#" << "IronRuby" << "IronPython" << "F#"

Working with Lists

Everything is an object (Literally !!)

1 3.times { puts "Microsoft is Evil" }2 Microsoft is Evil3 Microsoft is Evil4 Microsoft is Evil5 => 3 jruby-1.6.5 :011 > "Something".class => String jruby-1.6.5 :012 > 22.class => Fixnum

Meta Programming

jruby-1.6.5 :002 > eval("puts 'I can Program Myself'")I can Program Myself => nil

If Ruby is so good then why Java ?

If ruby is so good then why Java ?

● Java wins hands down in the Performance department

If ruby is so good then why Java ?

● True Multi-Threading

Getting JRuby

Method 1:● Install MRI and RVM from

○ Package manager (Linux only)○ ruby-lang.org

● Then do "rvm install jruby"

Getting JRuby

Method 2 :● Get it Directly from jruby.org

Running JRuby Programs

● jruby Foo.rb ● java -jar jruby-complete.jar Foo.rb

AOT compilation of Jruby

● jrubyc Foo.rb● java -jar jruby-complete.jar foo.class

Using java classes in Jruby1 include Java 2 java_import java.lang.Thread 3 java_import java.lang.System 4 class SomeClass 5 include java.lang.Runnable 6 def run 7 5.times do 8 System.out.println "Executing Thread : #{Thread.current_thread.get_id}" 9 end10 end11 end12 5.times{13 Thread.new(SomeClass.new).start14 }

Now onto Sinatra

● Sinatra is a DSL for quickly creating web-apps with Ruby

● http://sinatrarb.com ● Best Installed as "gem install sinatra"

Sinatra saying Hello

1 require 'rubygems'2 require 'sinatra'3 require 'data_mapper'4 set :bind , '0.0.0.0'5 get '/' do6 "Hello there this is Frank Sinatra from beyond the grave"7 end

Thank You