Note: I have written this post, like 3 weeks ago, but never had the time to actually finish it. Today, I am staying sick at home and while I definitely don't feel in shape for doing work on any of my projects, I JUST CAN'T GET AWAY FROM MY PC. I hope you will enjoy it. Here it goes...
At the beginning...
I started my journey as a programmer on static language as many other programmers did. The first language that I learned was Visual Basic 6.0 (in my high school years). I then continued my exploration with C, C++, Java and C#. Around my 3rd year at the university (studying Informatics), I had school web projects to make. This was the first time I was exposed to a dynamic language (PHP to be more specific).
"Exposed", sounds like I took a radiation hit.
Well, to be honest, by the time it pretty much seemed so. Getting used to the comfort of knowing so much about your code at compile time, has totally blinded me about the power of dynamic languages. It seemed so..."wrong" is the first word that comes to mind.
So, how did I started using DLs - long story short - I started developing for the web. I discovered JavaScript and the expressiveness it carries blew me away. So, dear reader, if you haven't tried any DL, yet - https://developer.mozilla.org/en/JavaScript/Guide Check this out!
Let us finally proceed on-topic​!
So, I decided to look at some unexplored by me DL.
My choice was Ruby.
Dynamic, reflective, programming language, designed by the Japanese computer scientist Yukihiro "Matz" Matsumoto.
Looking for tutorials I found this one:
This tutorial is amazing. It took about 30-40 minutes to finish (including the time I spent toying around with the exercises) and it's worth every second. I strongly recommend it if you want to get a feel of the language and the basic features it has.
I think, what makes this tutorial great, is the way they create interactivity - on the page you have a Ruby console, in which you can freely type-in and test the assignments, the examples and whatever language feature you want to try. I think I learned more for Ruby by playing outside the scope of the samples than actually doing the exercises (I guess this was part of the intention).
​What made a real good impression about Ruby
While I haven't gone deep in the language, nor any of the frameworks (rails), I spotted some pretty neat features.
The first thing that made impression to me, is the "everything-is-an-object" concept.
# Everything, including a literal, is an object, so this works: -199.abs # 199 "ice is nice".length # 11 "ruby is cool.".index("u") # 1 "Nice Day Isn't It?".downcase.split("").uniq.sort.join # " '?acdeinsty"
In JavaScript, there is almost the same notion. But you can't directly invoke methods on Number scalar type for example.
A feature that I haven't seen previously - method (function) returning more than one result. Here is an example of creating a getter and setter to a variable:
def create_set_and_get(initial_value=0) closure_value = initial_value return Proc.new {|x| closure_value = x}, Proc.new { closure_value } end setter, getter = create_set_and_get # ie. returns two values
This masks a little bit, one other feature, which will be more obvious in the next example:
def initialize(name, age) @name, @age = name, age end
Multiple assignment. It makes so much sense, yet, non of the languages I used implements it.
One example at http://en.wikipedia.org/wiki/Ruby_(programming_language) struck me the most - see how methods are created as extensions for the base type String:
COLORS = { :black => "000", :red => "f00", :green => "0f0", :yellow => "ff0", :blue => "00f", :magenta => "f0f", :cyan => "0ff", :white => "fff" } class String COLORS.each do |color,code| define_method "in_#{color}" do "<span style=\"color: ##{code}\">#{self}</span>" end end end "Hello, World!".in_blue # => "<span style=\"color: #00f\">Hello, World!</span>"
In the class String (extending the base type String) we are iterating through an associative array (hash in Ruby's terminology) and creating methods from every couple provided.
That's what I am talking about when I say that dynamic languages are expressive.
What made a bad impression
If I didn't liked something I learned about Ruby - it's the method/function parameters listing. It can be done with or without parentheses, i.e.:
def initialize(name, age) @name, @age = name, age end
or
def initialize name, age @name, @age = name, age end
Same goes for calling method/function. It looks confusing to me.
But then again - I am just a tourist not a local. :)
Summary
I think it definitely worth checking Ruby out. Many unique concepts and very expressive code.