The document compares and contrasts features of the Scala and Ruby programming languages, including type systems, pattern matching, monkey patching, dynamic calls, traits/modules, and more. It discusses how each language handles the given features, often providing code examples, and notes similarities and differences between the two approaches. The overall assessment is that a draw is the best way to characterize the comparison between Scala and Ruby, as both languages have their merits for different use cases and preferences.
1 of 152
Downloaded 597 times
More Related Content
Scala vs Ruby
1. SCALA VS RUBY
Rémy-Christophe Schermesser
rcs@octo.com
@El_Picador
1
52. 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]
end
end
22
53. 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]
end
end
22
54. gem install case
require 'case'
Scala
def matchTest x
case x
when 1
Ruby
3 2
"one"
when "two"
2
when Case::All[Integer]
"ruby.Integer"
when Case::Array[2, Case::Any]
x[1..-1]
end
end
22
71. Scala 2.9
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
}
}
27
72. Scala 2.9
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 Animal
animal.qualk // => Animal says qualk
animal.say("hello") // => Animal wants to say hello
27
73. Scala 2.9
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 Animal
animal.qualk // => Animal says qualk
animal.say("hello") // => Animal wants to say hello
27
74. Scala 2.9
Scala
class Animal extends Dynamic {
Ruby
def _select_(name: String) = println("Animal says " + name)
def _invoke_(name: String, args: Any*) = {
4 4
println("Animal wants to " + name + args.mkString(", "))
this
}
}
val animal = new Animal
animal.qualk // => Animal says qualk
animal.say("hello") // => Animal wants to say hello
27
75. Traits !
trait PimpMyClass {
def myMethod = println("myMethod")
}
class IncludeTrait extends PimpMyClass
(new IncludeTrait).myMethod
28
76. Traits !
trait PimpMyClass {
def myMethod = println("myMethod")
}
class IncludeTrait extends PimpMyClass
(new IncludeTrait).myMethod
28
77. Modules !
module PimpMyClass
def my_method
puts "my_method"
end
end
class IncludeModule
include PimpMyClass
end
IncludeModule.new.my_method
29
78. Modules !
Scala
module PimpMyClass
def my_method Ruby
puts "my_method"
5 5
end
end
class IncludeModule
include PimpMyClass
end
IncludeModule.new.my_method
29
83. class Duck
def quack; end
def walk; end
end
31
84. class Duck
def quack; end
def walk; end class Platypus
end def quack; end
def walk; end
end
31
85. class Duck
def quack; end
def walk; end class Platypus
end def quack; end
def walk; end
end
def act_as_a_duck animal
animal.quack
animal.walk
end
31
86. class Duck
def quack; end
def walk; end class Platypus
end def quack; end
def walk; end
end
def act_as_a_duck animal
animal.quack
animal.walk duck = Duck.new
end platypus = Platypus.new
act_as_a_duck(duck)
act_as_a_duck(platypus)
31
87. class Duck {
def quack = ...
def walk = ...
}
32
88. class Duck {
def quack = ...
def walk = ... class Platypus {
} def quack = ...
def walk = ...
}
32
89. class Duck {
def quack = ...
def walk = ... class Platypus {
} def quack = ...
def walk = ...
}
def ActAsADuck(a: { def quack; def walk })= {
a.quack
a.walk
}
32
90. 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)
32
91. class Duck {
def quack = ...
def walk = ...
Scala Ruby
class Platypus {
} def quack = ...
def walk = ...
}
}
6
a.quack
a.walk
6
def ActAsADuck(a: { def quack; def walk })= {
val duck = new Duck
val platypus = new
Platypus
ActAsADuck(duck)
ActAsADuck(platypus)
32
102. describe "Get of Array" do
it "first returns the first element" do
array = [1, 2, 3]
array.first.should == 1
end
end
SPEC TESTING
37
103. describe "Get of Array" do
it "first returns the first element" do
array = [1, 2, 3]
array.first.should == 1
end
end
SPEC TESTING
describe("Get of List") {
it("(0) returns the first element") {
val array = List(1, 2, 3)
array(0) should be 1
}
}
37
104. 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
105. 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