Scala vs Ruby

152
SCALA VS RUBY Rémy-Christophe Schermesser [email protected] @El_Picador 1

description

A comparison between the programming languages Scala and Ruby

Transcript of Scala vs Ruby

Page 1: Scala vs Ruby

SCALA VS RUBYRémy-Christophe Schermesser

[email protected]@El_Picador

1

Page 2: Scala vs Ruby

2

Page 3: Scala vs Ruby

3

Page 4: Scala vs Ruby

4

Page 5: Scala vs Ruby

5

Page 6: Scala vs Ruby

6

Page 7: Scala vs Ruby

7

Page 8: Scala vs Ruby

<relationships> <ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <ejb-relationship-role> <!-- A => B --> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <multiplicity>One</multiplicity> <relationship-role-source> <ejb-name>A</ejb-name> </relationship-role-source> <cmr-field> <cmr-field-name>b</cmr-field-name> </cmr-field> </ejb-relationship-role> <ejb-relationship-role> <!-- B => A --> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <multiplicity>One</multiplicity> <relationship-role-source> <ejb-name>B</ejb-name> </relationship-role-source> </ejb-relationship-role> </ejb-relation></relationships>

8

Page 9: Scala vs Ruby

9

Page 10: Scala vs Ruby

Functionalprogramming

10

Page 11: Scala vs Ruby

Functionalprogramming

APIs

10

Page 12: Scala vs Ruby

Functionalprogramming

APIs

gems

10

Page 13: Scala vs Ruby

Functionalprogramming

APIs

gemsEasy to use

10

Page 14: Scala vs Ruby

11

Page 15: Scala vs Ruby

Functionalprogramming ?

12

Page 16: Scala vs Ruby

Functionalprogramming ?

12

Page 17: Scala vs Ruby

Functionalprogramming ?

APIs ?

12

Page 18: Scala vs Ruby

Functionalprogramming ?

APIs ?

12

Page 19: Scala vs Ruby

Functionalprogramming ?

APIs ?

gems ?

12

Page 20: Scala vs Ruby

Functionalprogramming ?

APIs ?

gems ?

12

Page 21: Scala vs Ruby

Functionalprogramming ?

APIs ?

gems ?Easy to use ?

12

Page 22: Scala vs Ruby

Functionalprogramming ?

APIs ?

gems ?Easy to use ?

12

Page 23: Scala vs Ruby

VS

13

Page 24: Scala vs Ruby

VSFIGHT13

Page 25: Scala vs Ruby

VSDRAWSort of ...

13

Page 26: Scala vs Ruby

WHY A DRAW ?

14

Page 27: Scala vs Ruby

WHY A DRAW ?

let’s talk about languages

14

Page 28: Scala vs Ruby

WHY A DRAW ?

let’s talk about languages

let’s frameworkise

14

Page 29: Scala vs Ruby

WHY A DRAW ?

let’s talk about languages

let’s frameworkise

let’s deploy

14

Page 30: Scala vs Ruby

WHY A DRAW ?

let’s talk about languages

let’s frameworkise

let’s meet people

let’s deploy

14

Page 31: Scala vs Ruby

15

Page 32: Scala vs Ruby

LET’S TALK ABOUT LANGUAGES

15

Page 33: Scala vs Ruby

16

Page 34: Scala vs Ruby

BEST THING ABOUT

THE

SCALA & RUBY16

Page 35: Scala vs Ruby

No

at the end of lines

17

Page 36: Scala vs Ruby

No

at the end of lines;

17

Page 37: Scala vs Ruby

No

at the end of lines;

Scala1

Ruby1

17

Page 38: Scala vs Ruby

18

Page 39: Scala vs Ruby

λ18

Page 40: Scala vs Ruby

list.filter(_ % 2 == 0)

list.filter { e: Int => (e % 2 == 0))}

19

Page 41: Scala vs Ruby

list.filter(_ % 2 == 0)

list.select do |e| e % 2 == 0end

list.filter { e: Int => (e % 2 == 0))}

19

Page 42: Scala vs Ruby

list.filter(_ % 2 == 0)

list.select do |e| e % 2 == 0end

list.filter { e: Int => (e % 2 == 0))}

Scala2

Ruby2

19

Page 43: Scala vs Ruby

TYPES !

20

Page 44: Scala vs Ruby

STATICTYPES !

20

Page 45: Scala vs Ruby

var hash = new HashMap[Int, String]

STATICTYPES !

20

Page 46: Scala vs Ruby

var hash = new HashMap[Int, String]

STATICTYPES !

hash = Hash.newhash = 3

20

Page 47: Scala vs Ruby

var hash = new HashMap[Int, String]

STATICTYPES !

hash = Hash.newhash = 3

20

Page 48: Scala vs Ruby

var hash = new HashMap[Int, String]

STATICTYPES !Scala

2Ruby

2hash = Hash.newhash = 3

20

Page 49: Scala vs Ruby

PATTERN MATCHING

21

Page 51: Scala vs Ruby

gem install case

22

Page 52: Scala vs Ruby

gem install case

require 'case'

def matchTest x case x when 1 "one" when "two" 2 when Case::All[Integer] "ruby.Integer" when Case::Array[2, Case::Any] x[1..-1] endend

22

Page 53: Scala vs Ruby

gem install case

require 'case'

def matchTest x case x when 1 "one" when "two" 2 when Case::All[Integer] "ruby.Integer" when Case::Array[2, Case::Any] x[1..-1] endend

22

Page 54: Scala vs Ruby

gem install case

require 'case'

def matchTest x case x when 1 "one" when "two" 2 when Case::All[Integer] "ruby.Integer" when Case::Array[2, Case::Any] x[1..-1] endend

Scala3

Ruby2

22

Page 55: Scala vs Ruby

MONKEY PATCHING

23

Page 56: Scala vs Ruby

puts "a".to_s # => a

24

Page 57: Scala vs Ruby

puts "a".to_s # => a

class String def to_s "Monkey !" end def my_method "Patch !" endend

24

Page 58: Scala vs Ruby

puts "a".to_s # => a

class String def to_s "Monkey !" end def my_method "Patch !" endend

puts "a".to_s # => Monkey !

24

Page 59: Scala vs Ruby

puts "a".to_s # => a

class String def to_s "Monkey !" end def my_method "Patch !" endend

puts "a".to_s # => Monkey !

puts "a".my_method # => Patch !

24

Page 60: Scala vs Ruby

25

Page 61: Scala vs Ruby

Implicit !

25

Page 62: Scala vs Ruby

class MySuperString(original: String) { def myMethod = "Patch !"}

Implicit !

25

Page 63: Scala vs Ruby

class MySuperString(original: String) { def myMethod = "Patch !"}

Implicit !

implicit def string2super(x: String) = new MySuperString(x)

25

Page 64: Scala vs Ruby

class MySuperString(original: String) { def myMethod = "Patch !"}

Implicit !

println("a".myMethod) // => Patch !

implicit def string2super(x: String) = new MySuperString(x)

25

Page 65: Scala vs Ruby

class MySuperString(original: String) { def myMethod = "Patch !"}

Implicit !

println("a".myMethod) // => Patch !

implicit def string2super(x: String) = new MySuperString(x)

Scala3

Ruby3

25

Page 66: Scala vs Ruby

Dynamic calls

26

Page 67: Scala vs Ruby

class Animal def method_missing name, *args if args.empty? puts "Animal says " + name.to_s else puts "Animal wants to " + name.to_s + args.join(", ") end self end end

Dynamic calls

26

Page 68: Scala vs Ruby

class Animal def method_missing name, *args if args.empty? puts "Animal says " + name.to_s else puts "Animal wants to " + name.to_s + args.join(", ") end self end end

Dynamic calls

animal = Animal.new

animal.qualk # => Animal says : qualks !animal.say("hello") # => Animal wants to say hello

26

Page 69: Scala vs Ruby

27

Page 70: Scala vs Ruby

Scala 2.9

27

Page 71: Scala vs Ruby

class Animal extends Dynamic { def _select_(name: String) = println("Animal says " + name)

def _invoke_(name: String, args: Any*) = { println("Animal wants to " + name + args.mkString(", ")) this } }

Scala 2.9

27

Page 72: Scala vs Ruby

class Animal extends Dynamic { def _select_(name: String) = println("Animal says " + name)

def _invoke_(name: String, args: Any*) = { println("Animal wants to " + name + args.mkString(", ")) this } }

val animal = new Animalanimal.qualk // => Animal says qualkanimal.say("hello") // => Animal wants to say hello

Scala 2.9

27

Page 73: Scala vs Ruby

class Animal extends Dynamic { def _select_(name: String) = println("Animal says " + name)

def _invoke_(name: String, args: Any*) = { println("Animal wants to " + name + args.mkString(", ")) this } }

val animal = new Animalanimal.qualk // => Animal says qualkanimal.say("hello") // => Animal wants to say hello

Scala 2.9

27

Page 74: Scala vs Ruby

class Animal extends Dynamic { def _select_(name: String) = println("Animal says " + name)

def _invoke_(name: String, args: Any*) = { println("Animal wants to " + name + args.mkString(", ")) this } }

val animal = new Animalanimal.qualk // => Animal says qualkanimal.say("hello") // => Animal wants to say hello

Scala 2.9

Scala4

Ruby4

27

Page 75: Scala vs Ruby

Traits !

trait PimpMyClass { def myMethod = println("myMethod")}

class IncludeTrait extends PimpMyClass

(new IncludeTrait).myMethod

28

Page 76: Scala vs Ruby

Traits !

trait PimpMyClass { def myMethod = println("myMethod")}

class IncludeTrait extends PimpMyClass

(new IncludeTrait).myMethod

28

Page 77: Scala vs Ruby

Modules !module PimpMyClass def my_method puts "my_method" endend

class IncludeModule include PimpMyClassend

IncludeModule.new.my_method

29

Page 78: Scala vs Ruby

Modules !module PimpMyClass def my_method puts "my_method" endend

class IncludeModule include PimpMyClassend

IncludeModule.new.my_method

Scala5

Ruby5

29

Page 79: Scala vs Ruby

DUCK TYPING

30

Page 80: Scala vs Ruby

DUCK TYPING

It quacks !

30

Page 81: Scala vs Ruby

DUCK TYPING

It quacks !

It walks !

30

Page 82: Scala vs Ruby

DUCK TYPING

It quacks !

It walks !

30

Page 83: Scala vs Ruby

class Duck def quack; end def walk; endend

31

Page 84: Scala vs Ruby

class Duck def quack; end def walk; endend

class Platypus def quack; end def walk; endend

31

Page 85: Scala vs Ruby

class Duck def quack; end def walk; endend

class Platypus def quack; end def walk; endend

def act_as_a_duck animal animal.quack animal.walkend

31

Page 86: Scala vs Ruby

class Duck def quack; end def walk; endend

class Platypus def quack; end def walk; endend

def act_as_a_duck animal animal.quack animal.walkend

duck = Duck.newplatypus = Platypus.new

act_as_a_duck(duck)act_as_a_duck(platypus)

31

Page 91: Scala vs Ruby

class Duck { def quack = ... def walk = ...}

class Platypus { def quack = ... def walk = ...}

def ActAsADuck(a: { def quack; def walk })= { a.quack a.walk} val duck = new Duck

val platypus = new Platypus

ActAsADuck(duck)ActAsADuck(platypus)

Scala6

Ruby6

32

Page 92: Scala vs Ruby

≃33

Page 93: Scala vs Ruby

Only learn the syntax≃

33

Page 94: Scala vs Ruby

34

Page 95: Scala vs Ruby

LET’S FRAMEWORKISE

34

Page 96: Scala vs Ruby

35

Page 97: Scala vs Ruby

rSpec

Test::Unit

35

Page 98: Scala vs Ruby

UNIT TESTING

36

Page 99: Scala vs Ruby

UNIT TESTING

test "my test" do array = [1, 2, 3] assert_equal 1, array.firstend

36

Page 100: Scala vs Ruby

UNIT TESTING

test "my test" do array = [1, 2, 3] assert_equal 1, array.firstend

@Test def myTest() { val array = List(1, 2, 3) assert(array(0) === 1)}

36

Page 101: Scala vs Ruby

SPEC TESTING

37

Page 102: Scala vs Ruby

SPEC TESTING

describe "Get of Array" do it "first returns the first element" do array = [1, 2, 3] array.first.should == 1 endend

37

Page 103: Scala vs Ruby

SPEC TESTING

describe "Get of Array" do it "first returns the first element" do array = [1, 2, 3] array.first.should == 1 endend

describe("Get of List") { it("(0) returns the first element") { val array = List(1, 2, 3) array(0) should be 1 }}

37

Page 104: Scala vs Ruby

BDD TESTING

Feature: The user can get an element off an array Scenario: first is invoked on the array Given a non-empty array When first is invoked on the array Then the first element should be returned

38

Page 105: Scala vs Ruby

BDD TESTING

Feature: The user can get an element off an array Scenario: first is invoked on the array Given a non-empty array When first is invoked on the array Then the first element should be returned

38

Page 106: Scala vs Ruby

rSpec

Test::Unit

39

Page 107: Scala vs Ruby

rSpec

Test::Unit

Scala7

Ruby7

39

Page 108: Scala vs Ruby

40

Page 109: Scala vs Ruby

40

Page 110: Scala vs Ruby

40

Page 111: Scala vs Ruby

40

Page 112: Scala vs Ruby

41

Page 113: Scala vs Ruby

24 000gems

41

Page 114: Scala vs Ruby

gems

12 000for Rails

41

Page 115: Scala vs Ruby

gems

12 000for Rails

41

Page 116: Scala vs Ruby

gems

12 000for Rails

Scala7

Ruby8

41

Page 117: Scala vs Ruby

42

Page 118: Scala vs Ruby

Actors in Ruby ?

43

Page 119: Scala vs Ruby

Don’t try this at home !

43

Page 120: Scala vs Ruby

Don’t try this at home !

Scala8

Ruby8

43

Page 121: Scala vs Ruby

44

Page 122: Scala vs Ruby

45

Page 123: Scala vs Ruby

LET’S DEPLOY

45

Page 124: Scala vs Ruby

46

Page 125: Scala vs Ruby

rails new myappheroku create myappgit push heroku master

46

Page 126: Scala vs Ruby

rails new myappheroku create myappgit push heroku master

http://myapp.heroku.com

46

Page 127: Scala vs Ruby

rails new myappheroku create myappgit push heroku master

Scala8

Ruby9

http://myapp.heroku.com

46

Page 128: Scala vs Ruby

47

Page 129: Scala vs Ruby

47

Page 130: Scala vs Ruby

47

Page 131: Scala vs Ruby

47

Page 132: Scala vs Ruby

47

Page 133: Scala vs Ruby

47

Page 134: Scala vs Ruby

47

Page 135: Scala vs Ruby

47

Page 136: Scala vs Ruby

0

20

40

60

80

2,07

39,74

70,74

Source : http://shootout.alioth.debian.org

Average performance(less is better)

48

Page 137: Scala vs Ruby

0

20

40

60

80

2,07

39,74

70,74

Scala JRuby Ruby 1.9

Source : http://shootout.alioth.debian.org

Average performance(less is better)

48

Page 138: Scala vs Ruby

0

20

40

60

80

2,07

39,74

70,74

Scala JRuby Ruby 1.9

Source : http://shootout.alioth.debian.org

Average performance(less is better)

Scala9

Ruby9

48

Page 139: Scala vs Ruby

Do not talk of Ruby

to an admin49

Page 140: Scala vs Ruby

Do not talk of Ruby

to an adminNeither of Java

49

Page 141: Scala vs Ruby

50

Page 142: Scala vs Ruby

LET’S MEET PEOPLE

50

Page 143: Scala vs Ruby

51

Page 144: Scala vs Ruby

51

Page 145: Scala vs Ruby

52

Page 146: Scala vs Ruby

52

Page 147: Scala vs Ruby

«Most Java Programmers are Morons»

53

Page 148: Scala vs Ruby

«Most Java Programmers are Morons»

© Rails community

53

Page 149: Scala vs Ruby

Scala9

Ruby9

54

Page 150: Scala vs Ruby

?

?55

Page 151: Scala vs Ruby

?

?55

Page 152: Scala vs Ruby

56