43
(track sponsor) Amir Barylko Jruby & Iron Ruby Monday, June 13, 2011

PRDC11-Jruby-ironruby

Embed Size (px)

DESCRIPTION

Presentation done at PRDC Regina

Citation preview

Page 1: PRDC11-Jruby-ironruby

(track sponsor)

Amir Barylko

Jruby &Iron Ruby

Monday, June 13, 2011

Page 2: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

AMIR BARYLKOJRUBY & IRON RUBY

PRAIRIE DEV CONJUN 2011

Monday, June 13, 2011

Page 3: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

WHO AM I?

• Architect

• Developer

•Mentor

• Great cook

• The one who’s entertaining you for the next while!

Monday, June 13, 2011

Page 4: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

CONTACT AND MATERIALS

• Contact me: [email protected], @abarylko

• Blog:http://www.orthocoders.com

• Site:http://www.maventhought.com

• Download: http://www.orthocoders.com/presentations.

Monday, June 13, 2011

Page 5: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

RUBY INTRODynamic languages

ToolboxFeatures

Monday, June 13, 2011

Page 6: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

DYNAMIC LANGUAGES

High level

Dynamically typed

Runtime over compile time

Closures

Reflection

Platform independent

Monday, June 13, 2011

Page 7: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

DEVELOPERS TOOLBOX

•Make your toolbox grow!

• The right tool for the job

• Not a replacement

• Combine strengths

• Problem solving

Monday, June 13, 2011

Page 8: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

WELCOME TO RUBY

Created in mid-90s by “Matz” Matsumoto in Japan

Smalltalk, Perl influences

Dynamic typing

Object Oriented

Automatic memory management

Several implementations: MRI, YARB, JRuby

Totally free!!

Monday, June 13, 2011

Page 9: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

RUBY FEATURES

Everything is an expression

Metaprogramming

Closures

Garbage collection

Exceptions

Operator overloading, flexible syntax

Powerful standard library

Monday, June 13, 2011

Page 10: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

RUBY SUPPORT

Hundreds of books

User conferences all over the world

Active community (you can create a conference in your own city and top Ruby coders will go there to teach others, invite them and see)

Lots of great web sites: basecamp, twitter, 43 things, hulu, scribd, slideshare, Justin.tv

Lots of web frameworks inspired by Ruby on Rails

Monday, June 13, 2011

Page 11: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

RUBY LANGUAGEBasic Types

Control StructuresMethodsModifiers

Monday, June 13, 2011

Page 12: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

BASIC TYPES

Numbers

1.class => Fixnum

1.1.class => Float

(120**100).class => Bignum

3.times {puts “he “}

Monday, June 13, 2011

Page 13: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

BASIC TYPES II

• Strings

'he ' + 'he' => he he

“That's right” => That's right

'He said “hi”' => He said “hi”

“He said \“hi\”” => He said “hi”

“1 + 1 is #{1+1}” => 1 + 1 is 2

"#{'Ho! '*3}Merry Christmas" =>Ho! Ho! Ho! Merry

Christmas

Monday, June 13, 2011

Page 14: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

BASIC TYPES III

Arrays

a = [1, 5.5, “nice!”]

1 == a.first

1 == a[0]

nil == a[10]

a[1] = 3.14

a.each {|elem| puts elem}

a.sort

Monday, June 13, 2011

Page 15: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

BASIC TYPES IV

• Hashesh = {“one” => 1, 1 => “one”}

h[“one”] == 1

h[1] == “one”

h[“two”] == nil

h.keys == [“one”, 1] (or is it [1, “one”] ?)

h.values == [“one”, 1] (or is it [1, “one”] ?)

h[“one”] = 1.0

Monday, June 13, 2011

Page 16: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

BASIC TYPES V

Symbols: constant names. No need to declare, guaranteed uniqueness, fast comparison

:apple == :apple

:orange != :banana

[:all, :those, :symbols]

{:ca => “Canada”, :ar => “Argentina”, :es => “Spain”}

Monday, June 13, 2011

Page 17: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

CONTROL STRUCTURES

if

while

if count < 20 puts “need more”elsif count < 40 puts “perfect”else puts “too many”end

while count < 100 && need_more buy(1)end

Monday, June 13, 2011

Page 18: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

CONTROL STRUCTURES II

Statement modifiers

buy while need_more?

buy(5) if need_more?

buy until left == 0

buy unless left < 5

Monday, June 13, 2011

Page 19: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

CONTROL STRUCTURES III

• Case

case left

when 0..5

dont_buy_more

when 6..10

buy(1)

when 10..100

buy(5)

end

Monday, June 13, 2011

Page 20: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

METHODS

Simple

Default arguments

def play(movie_path)....end

def play(movie_path, auto_start = true, wrap = false)....end

Monday, June 13, 2011

Page 21: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

METHODS II

Return value: the last expression evaluated, no need for explicit return

No need for parenthesis on call without arguments (same syntax to call a method and a field)

def votes(voted, num_votes) voted && num_votes || nilend

buy() == buy

movie.play() == movie.play

Monday, June 13, 2011

Page 22: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

METHODS III

No need also with arguments (but careful!! only if you know what you are doing)

movie.play “Pulp fiction”, false, true

Monday, June 13, 2011

Page 23: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

RUBY LANGUAGE IIClassesMixin

Enumerable

Monday, June 13, 2011

Page 24: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

CLASSES & OBJECTS

Initializer and instance variablesclass Movie def initialize(name) @name = name end

def play puts %Q{Playing “#{@name}”. Enjoy!} endend

m = Movie.new(“Pulp fiction”)m.play

=> Playing “Pulp fiction”. Enjoy!

Monday, June 13, 2011

Page 25: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

CLASSES & OBJECTS II Attributesclass Movie def initialize(name) @name = name end

def name @name end

def name=(value) @name = value end

end

m = Movie.new('Brazil').name = “Pulp fiction”

# attr_reader :name

# attr_writter :name

# attr_accessor :name}

Monday, June 13, 2011

Page 26: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

CODE ORGANIZATION

Code in files with .rb extension

Require 'movie' will read movie.rb file and make its methods available to the current file

Require 'media/movie' will read file from media dir relative to the current working dir$LOAD_PATH << 'media'require 'movie'

Monday, June 13, 2011

Page 27: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

CODE ORGANIZATION II

Relative to this file:require File.join(File.dirname(__FILE__), 'media/movie')

Monday, June 13, 2011

Page 28: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

MIXINS

What about module “instance methods”?

One of the greatest Ruby features!

You can define functions in Modules, and get them added to your classes.

Great code reuse,

Multiple inheritance alternative.

Code organization

Monday, June 13, 2011

Page 29: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

ENUMERABLE

Enumerable mixin, from the standard library documentation:

The Enumerable mixin provides collection

classes with several traversal and

searching methods, and with the ability to

sort. The class must provide a method each,

which yields successive members of the

collection

Monday, June 13, 2011

Page 30: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

ENUMERABLE II

It provides useful methods such as:

map

to_a

take_while

count

inject

Monday, June 13, 2011

Page 31: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

EXAMPLESrSpec

EnumerableMethod Missing

SinatraBDD Cucumber

DSL

Monday, June 13, 2011

Page 32: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

RSPEC TESTING LIBRARY

require 'java'require 'rubygems'

java_import 'MovieLibrary'java_import 'Movie'

describe MovieLibrary do

it "should be created empty" do lib = MovieLibrary.new lib.contents.should be_empty end it "should add an element" do lib = MovieLibrary.new m = Movie.new 'Blazing Saddles' lib.add m lib.contents.should include(m) lib.contents.count.should == 1 end end

Monday, June 13, 2011

Page 33: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

ENUMERBLE MIXIN

require 'rubygems'java_import 'MovieLibrary'java_import 'Movie'

class MovieLibrary include Enumerable def each(&block) contents.each(&block) endend

Monday, June 13, 2011

Page 34: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

ENUMERBLE MIXIN

lib = MovieLibrary.new

lib.add(Movie.new "Blazing Saddles")lib.add(Movie.new "Spaceballs")lib.add(Movie.new "Young Frankenstein")

lib.each { |m| puts "The movie is #{m.title}"}

puts "\n\n**** Using Inject"lib.inject([]) { |a, m| a << m.title } .each { |s| puts "The movie is #{s}" }

puts "\n\n*** Using find"lib.find_all { |m| m.title.end_with? 's' } .each { |m| puts "The movies that end with s: #{m.title}"}

Monday, June 13, 2011

Page 35: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

EXTEND LIBRARYWITH METHOD MISSING

require File.dirname(__FILE__) + "....bin/Debug/MavenThought.MovieLibrary.dll"require 'rubygems'include MavenThought::MovieLibrary

class Library def method_missing(m, *args) if m.id2name.include?("find_by") field = m.id2name.sub( /find_by_/, "") contents.find_all( lambda { |m| m.send(field) == args[0] } ) else super end end end

lib = Library.new

lib.add Movie.new( "Blazing Saddles", System::DateTime.new(1972, 1, 1))lib.add Movie.new( "Spaceballs", System::DateTime.new(1987, 1, 1))

puts "Using find by title #{lib.find_by_title( "Blazing Saddles" )}"

Monday, June 13, 2011

Page 36: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

SIMPLE WEB WITH SINATRA

require 'rubygems'require 'sinatra'require 'haml'require 'singleton'

java_import 'MovieLibrary'java_import 'Movie'

class MovieLibrary include Singletonend

# indexget '/' do @movies = MovieLibrary.instance.contents haml :indexend

# createpost '/' do m = Movie.new(params[:title]) MovieLibrary.instance.add m redirect '/'end

Monday, June 13, 2011

Page 37: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

BDD WITH CUCUMBER

Feature: Addition In order to make my library grow As a registered user I want to add movies to the library

Scenario: Add a movie Given I have an empty library When I add the following movies: | title | release_date | | Blazing Saddles | Feb 7, 1974 | | Young Frankenstein | Dec 15, 1974 | | Spaceballs | Jun 24, 1987 | Then The library should have 3 movies And "Blazing Saddles" should be in the list with date "Feb 7, 1974" And "Young Frankenstein" should be in the list with date "Dec 15, 1974" And "Spaceballs" should be in the list with release date "Jun 24, 1987"

Monday, June 13, 2011

Page 38: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

BDD WITH CUCUMBER

require File.dirname(__FILE__) + "...MavenThought.MovieLibrary.dll"

include MavenThought::MovieLibrary

Given /^I have an empty library$/ do @lib = Library.newend

When /^I add the following movies:$/ do |table| table.hashes.each do |row| movie = Movie.new row[:title], System::DateTime.parse(...) @lib.add movie endend

Monday, June 13, 2011

Page 39: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

DSL IRAKE

desc "Builds the project"task :build do call_target msbuild_cmd, :buildend

desc "Rebuild the application by cleaning and then building"task :rebuild => [:clean, :build]

desc "Runs all the tests"task :test => ["test:all"]

Monday, June 13, 2011

Page 40: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

DSL IICRON - WHENEVER

every 10.minutes do runner "MyModel.some_process" rake "my:rake:task" command "/usr/bin/my_great_command" end

every 2.days, :at => '4:30am' do command "/usr/bin/my_great_command" end

Monday, June 13, 2011

Page 41: PRDC11-Jruby-ironruby

QUESTIONS?

(Don’t be shy)

Monday, June 13, 2011

Page 42: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

CONTACT AND MATERIALS

• Contact me: [email protected], @abarylko

• Blog:http://www.orthocoders.com

• Site:http://www.maventhought.com

• Download: http://www.orthocoders.com/presentations.

Monday, June 13, 2011

Page 43: PRDC11-Jruby-ironruby

Amir Barylko - Jruby & Iron Ruby MavenThought Inc.

ONLINE RESOURCES

IronRuby: http://ironruby.net/

Jruby: http://jruby.org

Ruby language site: http://www.ruby-lang.org

rSpec: http://rspec.info/

Sinatra: http://www.sinatrarb.com/

Cucumber: http://cukes.info/

Rake: http://rake.rubyforge.org/

Monday, June 13, 2011