codemash-iron-ruby-match-made-in-heaven

42
Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011 AMIR BARYLKO IRON RUBY AND .NET A MATCH MADE IN HEAVEN CODEMASH JAN 2011 Thursday, January 13, 2011

Upload: amir-barylko

Post on 02-Nov-2014

686 views

Category:

Technology


0 download

DESCRIPTION

Presenta

TRANSCRIPT

Page 1: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

AMIR BARYLKOIRON RUBY AND .NET

A MATCH MADE INHEAVEN

CODEMASHJAN 2011

Thursday, January 13, 2011

Page 2: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

WHO AM I?

• Architect

• Developer

•Mentor

• Great cook

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

Thursday, January 13, 2011

Page 3: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

CONTACT AND MATERIALS

• Contact me: [email protected], @abarylko

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

Thursday, January 13, 2011

Page 4: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

RUBY INTRODynamic languages

TestingIRB

Constructs

Thursday, January 13, 2011

Page 5: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

DYNAMIC LANGUAGES

High level

Dynamically typed

Runtime over compile time

Closures

Reflection

Platform independent

Thursday, January 13, 2011

Page 6: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

.NET CLR

Iron Ruby DLR CLR

Thursday, January 13, 2011

Page 7: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

DEVELOPERS TOOLBOX

•Make your toolbox grow!

• The right tool for the job

• Not a replacement

• Combine strengths

• Problem solving

Thursday, January 13, 2011

Page 8: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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!!

Thursday, January 13, 2011

Page 9: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

RUBY FEATURES

Everything is an expression

Metaprogramming

Closures

Garbage collection

Exceptions

Operator overloading, flexible syntax

Powerful standard library

Thursday, January 13, 2011

Page 10: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

RUBY SUPPORT

Hundreds of books

User conferences all over the world

Active community (you can create a conf 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

Thursday, January 13, 2011

Page 11: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

SET UP

Download IronRuby installer

Put the bin folder on the path

That’s it!

Thursday, January 13, 2011

Page 12: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

INTERACTIVE IRON RUBY SHELL

c:\> ir.exe

> puts “hello”

hello

=> nil

> "Hello World! " * 2

=> "Hello World! Hello World!"

> ((1 + 5) * 3) ** 2

=> 324

> x = 1.upto(5).to_a

=> [1, 2, 3, 4, 5]

> x.join

=> "12345"

> (1..10).inject([]) { |a, i| a << i }

=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Thursday, January 13, 2011

Page 13: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

BASIC TYPES

Numbers

1.class => Fixnum

1.1.class => Float

(120**100).class => Bignum

3.times {puts “he “}

Thursday, January 13, 2011

Page 14: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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

Thursday, January 13, 2011

Page 15: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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

Thursday, January 13, 2011

Page 16: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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

Thursday, January 13, 2011

Page 17: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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”}

Thursday, January 13, 2011

Page 18: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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

Thursday, January 13, 2011

Page 19: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

CONTROL STRUCTURES II

Statement modifiers

buy while need_more?

buy(5) if need_more?

buy until left == 0

buy unless left < 5

Thursday, January 13, 2011

Page 20: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

CONTROL STRUCTURES III

• Case

case left

when 0..5

dont_buy_more

when 6..10

buy(1)

when 10..100

buy(5)

end

Thursday, January 13, 2011

Page 21: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

METHODS

Simple

Default arguments

def play(movie_path)....end

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

Thursday, January 13, 2011

Page 22: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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

Thursday, January 13, 2011

Page 23: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

METHODS III

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

movie.play “Pulp fiction”, false, true

Thursday, January 13, 2011

Page 24: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

RUBY INTRO IIClassesMixin

Enumerable

Thursday, January 13, 2011

Page 25: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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!

Thursday, January 13, 2011

Page 26: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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}

Thursday, January 13, 2011

Page 27: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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'

Thursday, January 13, 2011

Page 28: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

CODE ORGANIZATION II

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

Thursday, January 13, 2011

Page 29: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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

Thursday, January 13, 2011

Page 30: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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

Thursday, January 13, 2011

Page 31: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

ENUMERABLE II

It provides useful methods such as:

map

to_a

take_while

count

inject

Thursday, January 13, 2011

Page 32: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

EXAMPLESrSpec

Enumerable Mixinmissing_method

SinatraBDD Cucumber

DSL

Thursday, January 13, 2011

Page 33: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

RSPEC TESTING LIBRARY

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

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

Thursday, January 13, 2011

Page 34: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

EXTEND LIBRARYWITH METHOD MISSING

require File.dirname(__FILE__) + "/../main/MavenThought.MovieLibrary/bin/Debug/MavenThought.MovieLibrary.dll"

require 'rubygems'

include MavenThought::MovieLibrary

# Extend library to use method missing to add find_byclass 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

l = Library.new

l.add Movie.new('Blazing Saddles', System::DateTime.new(1972, 1, 1))l.add Movie.new('Spaceballs', System::DateTime.new(1984, 1, 1))

puts "Found the movie #{l.find_by_title 'Spaceballs'}"puts "Found the movie #{l.find_by_release_date System::DateTime.new(1972, 1, 1)}"

Thursday, January 13, 2011

Page 35: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

SIMPLE WEB WITH SINATRA

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

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

class Library include Singletonend

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

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

Thursday, January 13, 2011

Page 36: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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 release date "Feb 7, 1974" And "Young Frankenstein" should be in the list with release date "Dec 15, 1974" And "Spaceballs" should be in the list with release date "Jun 24, 1987"

Thursday, January 13, 2011

Page 37: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

CUCUMBER STEPS

require File.dirname(__FILE__) + "/../../main/MavenThought.MovieLibrary/bin/Debug/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(row[:release_date]) @lib.add movie endend

Then /^The library should have (.*) movies$/ do |count| @lib.contents.count.should == count.to_iend

Then /^"([^\"]*)" should be in the list with release date "([^\"]*)"$/ do |title, release| @lib.contents.find( lambda { |m| m.title == title and m.release_date == System::DateTime.parse(release) } ).should_not be_nilend

Thursday, January 13, 2011

Page 38: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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"]

Thursday, January 13, 2011

Page 39: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

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

Thursday, January 13, 2011

Page 40: Codemash-iron-ruby-match-made-in-heaven

QUESTIONS?

(Don’t be shy)

Thursday, January 13, 2011

Page 41: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - RoR Training MavenThought Inc. - June 2010

CONTACT AND MATERIALS

• Contact me: [email protected], @abarylko

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

Thursday, January 13, 2011

Page 42: Codemash-iron-ruby-match-made-in-heaven

Amir Barylko - Iron Ruby and .NET MavenThought Inc. - Jan 2011

ONLINE RESOURCES

IronRuby: http://ironruby.net/

The Ruby Bible (a.k.a. Pickaxe): http://ruby-doc.org/docs/ProgrammingRuby/

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/

Thursday, January 13, 2011