Just looking at Ruby again. My first encounter with Ruby like most people is through ROR, it was a few years back (era 1.x or maybe before that). It was a very nice language and at that time, it wasn't that popular. I did however convince a colleague of mine to push ROR with the help of Bruce Tate's
From Java To Ruby book. He was successful in getting ROR into a Java EE environment. I had a quick glance of Ruby again today and in tutorial there was this code:
class MegaGreeter
attr_accessor :names
# Create the object
def initialize(names = "World")
@names = names
end
# Say hi to everybody
def say_hi
if @names.nil?
puts "..."
elsif @names.respond_to?("each")
# @names is a list of some kind, iterate!
@names.each do |name|
puts "Hello #{name}!"
end
else
puts "Hello #{@names}!"
end
end
# Say bye to everybody
def say_bye
if @names.nil?
puts "..."
elsif @names.respond_to?("join")
# Join the list elements with commas
puts "Goodbye #{@names.join(", ")}. Come back soon!"
else
puts "Goodbye #{@names}. Come back soon!"
end
end
end
if __FILE__ == $0
mg = MegaGreeter.new
mg.say_hi
mg.say_bye
# Change name to be "Zeke"
mg.names = "Zeke"
mg.say_hi
mg.say_bye
# Change the name to an array of names
mg.names = ["Albert", "Brenda", "Charles",
"Dave", "Englebert"]
mg.say_hi
mg.say_bye
# Change to nil
mg.names = nil
mg.say_hi
mg.say_bye
end
It's nice and concise, however it doesn't stand out as much as before. My comparison was Java, Tcl and Perl a few years ago. I then did a quick copy and paste, translated it to Scala.
class MegaGreeter(var names: Any = List("World")) {
def say_hi {
names match {
case null => println("...")
case listOfNames: List[String] => listOfNames foreach (n => println("Hello " + n))
case _ => println("Hello " + names)
}
}
def say_bye {
names match {
case null => println("...")
case listOfNames: List[String] => println("Goodbye " + listOfNames mkString(",") + ". Come back soon!")
case _ => println("Goodbye " + names + ". Come back soon!")
}
}
}
object MegaGreeter {
def main(args: Array[String]) {
val mg = new MegaGreeter
mg.say_hi
mg.say_bye
// Change name to be "Zeke"
mg.names = "Zeke"
mg.say_hi
mg.say_bye
// Change the name to an array of names
mg.names = List("Albert", "Brenda", "Charles",
"Dave", "Englebert")
mg.say_hi
mg.say_bye
// Change to nil
mg.names = null
mg.say_hi
mg.say_bye
}
}
Above is not really Scala idiomatic, but close to the original Ruby code especially how the class is created and used. Ruby still looks nice, but not as shiny as a few years ago. I am not here to say Scala is better than Ruby, but more of what is shiny before may not be as shiny today. Just like Java was shiny when I first time used it about 1996, it seems these days people see Java as the wart language. Scala will loose its shiny appeal in the next few years too.
I also been using the term "shiny", as its really about appeal. I still use Java and occasionally Perl, even if the are not shiny anymore. These older languages are still great in their own right. C is still cool these days.. I think.