I keep spreading the message. While Java gurus are still faithing on those religious best web framework wars , there is hidden winner already.
The best Java web framework award goes to … drumrolls … Finland.
Vaadin – When I saw it for a first time few months back, I was shocked. That what Java is meant to be – Innovative, slick, sexy.
On this blog the Groovy inventor talks on Scala: Scala as the long term replacement for java/javac?
I am pretty sure web is full of that link, but it a quite an interesting read. So simply spreading the message.
Let’s get back to the Ruby’s Shape , Triangle, Square polymorphism example and translate it to Scala:
object HelloObjects extends Application {
val shapes = new Array[Shape](3);
shapes(0) = new Triangle(7,7)
shapes(1) = new Square(7)
shapes(2) = new Circle(7)
shapes.foreach(shape => shape.info)
}
trait Shape {
def name(): String
def area(): Double
def info() = println(name() + " area: " + area())
}
class Triangle(x: Int, y: Int) extends Shape {
override def name() = "Triangle"
override def area() = x * y / 2.0
}
class Square(x: Int) extends Shape {
override def name() = "Square"
override def area() = x * x
}
class Circle(r: Int) extends Shape {
val PI = 22 / 7;
override def area() = PI * r
override def name() = "Circle"
}
Apparently Rails doesn’t scale and Twitter moved to a JVM compiled solution which is … Scala. Probably Twitter is moving to Lift altogether?
http://www.artima.com/scalazine/articles/twitter_on_scala.html
Coming from Java to Ruby … Have you ever encounter this?
a = -5
n = 3
x = (a % n)
In Java this would return 2.
In Ruby though it returns 1.
Reason. Ruby is using different algorythm for modulo:
If you still feel more comfortable with old habits, you can hack it using abs function:
a = -5
n = 3
x = (a.abs % n)
Which Ruby will evaluate to 2
Remember your first cup of Object Oriented Java? Shape, Triangle, Circle polymorphism?
Here it’s the kewl Ruby equivalent:
class Shape
def info
// prints abstract method name and area. Confusing eh?
puts "#{name} area: #{area}"
end
end
class Triangle < Shape
attr_reader :base, :height
def initialize(base, height)
super()
@base = base
@height = height
end
def area
return ((base * height).to_f./ 2)
end
def name
return "Triangle"
end
end
class Square < Shape
attr_reader :height
def initialize(height)
super()
@height = height
end
def area
return (height * height).to_f
end
def name
return "Square"
end
end
class Circle < Shape
attr_reader :r
PI = 22.to_f./7
def initialize(r)
super()
@r = r
end
def area
return (PI * r * r)
end
def name
return "Circle"
end
end
if __FILE__ == $0
g = Triangle.new(7, 7)
g.info
g = Square.new(7)
g.info
g = Circle.new(7)
g.info
end
Result is:
Triangle area: 24.5
Square area: 49.0
Circle area: 154.0


