Monday, August 13, 2012

Comparing Syntax



At this time, I see some similarities between coffeescript syntax and ruby syntax. Here's an example...

I created a script, a.coffee...
name = "jim"
greet = "Hello #{name}"

console.log greet

Running this on my terminal...
$ coffee a.coffee

Hello jim

To see how the a.coffee file comes out into javascript, I do...
$coffee -p a.coffee
...and I get...

(function() {
  var greet, name;
  name = "jim";
  greet = "Hello " + name;
  console.log(greet);
}).call(this);

If I were to write a.coffee into ruby, the script will come out like...
def greet
  name = "jim"
  greet = puts "Hello #{name}"
end

greet

See what I'm doing here?

I am teaching myself web development using ruby. At this time, I am learning Sinatra and now have a simple blog up running in heroku. So that I can spiffy up that blog, I am learning html5, css (SASS, or LESS), and javascript via coffeescript.

I am intimidated to learn javascript as is. I got spoiled with ruby syntax. I don't want to bore myself having to learn js with all those curly braces and semi-colons. As a workaround, I am teaching myself javascript by way of coffeescript, which, I think, is similar to ruby.

No comments:

Post a Comment