all repos — h3rald @ 19e4a5e256c7bbd728825af8bd1f16b7abbb9137

The sources of https://h3rald.com

content/articles/ruby-facets.textile

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
----- 
permalink: ruby-facets
filters_pre: 
- erb
- redcloth
title: A glance at Ruby facets
comments: []

date: 2007-07-01 12:11:00 +02:00
tags: 
- ruby
- review
- programming
type: article
toc: true
-----
_"Code is poetry"._

Oddly enough, this is a quote from WordPress.org[1], the home of the most popular blog platform on the Internet. Odd because WordPress is written in PHP, whose syntax — with all due respect —  is not the most elegant or poetic at all.
I've been programming in PHP for quite some time, I studied Pascal at high school, I learnt C++ at university, I used Java for my thesis and I know what Perl and Python look like. I never ventured in the Lisp world, it looks fascinating but a bit too esoteric for my linking.
I've been looking for a programming language of choice since my first C++ exam: _there must be a way_  — I thought — _to do all this in a much simpler way_. I was delighted to experiment languages like Java, which seemed to make life a little bit easier for developers, but not quite. It all comes to personal tastes, of course, but I always wanted a programming language which besides being powerful and flexible was also elegant and natural. 
That — I believe — is one of the reasons why we have a Ruby on Rails[2] and not a PHP on Rails framework (what happened afterwards is another story[3]), among other things.

When I first had a brief glance at Ruby[4], I knew I'd become addicted to it. First of all because it _looks_ nice, then, of course, because it is extremely versatile and its code is highly manageable.

This article does not intend to teach you how to program in Ruby, but rather it aims to show _some_ of Ruby's aspects to the curious among you. I'll try to describe, as someone who's just discovered the beauty of this language, quite a few of its many facets.

h3. Syntax Overview

Forget semicolons, you'll never use one ever again in Ruby, as there's no need to add them at the end of each line. Same goes with those annoying curly brackets: they are just used for delimiting hashes and blocks (more on all this later on).  Ruby uses _def_ and _end_  keywords instead: you'll never believe how this does _not_  add unnecessary verbosity to your code, but rather helps you manage it.
This is a relief for me especially, because on Italian keyboards you have to press Alt Gr+SHIFT+è to get an open curly bracket, which says it all. So let's take a look at our first silly Ruby function, shall we?

<% highlight :ruby do %>
def hello(name=nil)
  if name then
    print 'Hello, '+name+'!'
  else
    print 'Hello!'
  end
end
<% end %>






- optional parenthesis
- no semicolon
- def .. end
- no for, other loops

h3. Objects, Classes and Modules

- everything is an object
- nil
- open classes
- modules, mixins 
- everything has a value (returns not necessary)

h3. Variables

- no references
- multiple assignments
- hashes and arrays
- accessors
- symbols

h3. Methods

- alias
- ?, !
- method_missing
- *something

h3. Operators

- <<
- ||=, &&=
- =~
- redefine

h3. Blocks

-iterators
- lambda, Proc
- yield
- &

h3. Other Goodies

- irb
- exceptions
- marshalling
- RDoc, ri
- YAML

fn1. WordPress, blogging platform, "www.wordpress.org":http://www.wordpress.org

fn2. Ruby on Rails, web development framework, "www.rubyonrails.org":http://www.rubyonrails.org

fn3. For a list of frameworks inspired by Ruby on Rails, refer to "Rails-inspired PHP frameworks":http://www.h3rald.com/articles/rails-inspired-php-frameworks.

fn4. Ruby programming language, "www.ruby-lang.org":http://www.ruby-lang.org/en/