When I decided to start Javascript Kata, a friend of mine just thought it was a cool idea and started his own site, Ruby Fleebie. I've never written code in Ruby but it looks like a very pleasant language. He borrowed me Agile Web Development with Rails: A Pragmatic Guide and I really liked the look of the code (by the way Frank, I'll give it back to you soon, promise). But as I am reading Ruby Fleebie, I always see similarities with javascript.

A great knowledge of javascript won't get you a job. But knowing a “serious” language + great knowledge of javascript and you will be rich (… but maybe not). So, why not take Ruby as the “serious” language as it is the rising star these days?

So here's a first draft of the list of similarities.

1. Everyone can modify your class structure

A lot of “serious” developers think about this as a weakness but this is the greatest thing that happened to languages since object-oriented. No more protecting you from yourself. Just that great feeling of liberty (you know the one when you are on the deck of a boat by a starry night with Leonardo DiCaprio behind you).

Ruby : 3 steps to understand how classes and objects work in ruby Javascript : What are javascript prototypes? (short answer for advanced javascripters)

2. You can pass a function as a parameter

In javascript, any function can be passed as a parameter.

[source:javascript] // Create a function that alerts a number function showNumber(theNumber) { alert(theNumber); }

// Create a function that “shows” 42 using // It takes as parameter the function that will show the number function show42(theShowNumberFunction) { theShowNumberFunction(42); }

// Call to show 42 show42(showNumber); [/source]

In ruby, it is called code blocks.

[source:ruby] // Create a function that alerts a number def show_number(theNumber) puts theNumber end

// Create a function that “shows” 42 using def show_42() yield(42) end

def main show42() do | theNumber | shownumber(theNumber) end end

[/source]

Ruby : An introduction to code blocks Javascript : 3 ways of creating functions in javascript

3. Walk like a duck?

You want an object to do something (walk like a duck) but you don't know the type of object and you don't know if it can.

In javascript, no problem, you just check if the object has the function (now that we know that objects are javascript objects are hash-tables)

[source:javascript] // Create a Dog class without functions function Dog() { }

// Create a Cat class with a walkLikeADuck function function Cat() { }

Cat.prototype.walkLikeADuck = function() { // Code to walk like a duck }

var theDog = new Dog(); var theCat = new Cat();

// Check if they can walk like a duck // by using [theObject].[thePropertyOrMethod] alert(“The dog can walk like a duck? ” + (theDog.walkLikeADuck != null)); alert(“The cat can walk like a duck? ” + (theCat.walkLikeADuck != null)); [/source]

In Ruby, it is simple too! Just use the respond_to method.

[source:ruby]

… code for creating classes …

Check if they can walk like a duck

puts “The dog can walk like a duck? ” + (theDog.respondto?(:walkLikeADuck).tos) puts “The cat can walk like a duck? ” + (theCat.respondto?(:walkLikeADuck).tos) [/source]

You'll see that only the cat can walk like a duck.

Ruby : What’s the fuzz about ducktyping? Javascript : How to use javascript hashes (or hash-table)