Ruby - Scripting and Web Services
Ruby has been designed by Yukihiro "Matz" Matsumoto from 1993 to 1995.
The goal was to program in human style rather than adapting its mind to
the computer structure. This was firmed up by applying a principle of least
surprise that means that the language syntax is always as the programmer
does expect it is. But there is also a lot of conventional rules to simplify
the programming: only a way to do a thing.
It is interpreted (uses bytecode), fully object-oriented and dynamically
typed. Has been inspired by Perl, Smalltalk and Python.
Ruby on rails was released in 2004. This is an alternative to Ajax, easier
and more complete but requires the server-side part to be (re)written in
Ruby also.
Features
- By the trickery of the "main" object, variables and functions becomes attributes and methods.
- Single inheritance.
- Operator overloading.
- Closures and continuations.
- Threads, exceptions.
- Reflection.
- UTF-8.
The language
Syntax
Capitalized or uppercase identifiers are constants.
Floating point numbers are denoted by a dot followed by a number or zéro.
Variables:
- global variables start with $
- attributes in classes start with @
- local variables are simple identifiers.
Symbols
# starts a comment.
[ exp, ... ] enclose arrays.
{ 1 => a, ... } is for a dictionary.
Control structures
The if structure has elsif and else options.
if x < 10 then
print "x less than 10\n"
else
print "etc...\n"
end
The while structure:
while expr [do]
...
end
Function or method
The definition starts with the def keyword, plus the name and the
list or arguments separated by commas, then the statements and is terminated
by the end keyword.
The return keyword in the body of the definition allows to return
one or several values.
def funcname( arguments )
...statements... return x, y, z
end
Class
class name
...
end
Why use Ruby?
The language was locally used before the rails framework appears and shares its success with the language itself.
Sites
- Main
The interpreter may be downloaded on the main site. - Dmoz
Ruby in the open directory. - Ruby-doc
Documentation for Ruby only. - Ruby
On Rails
Main site for the successful framework.
Sample code
Hello world!
do puts "Hello, World!" end
Content of an array.
mylist = [ "d", "e", "m", "o" ]
for i in mylist do
print i, "\n"
end
References
The Ruby reference manual.
(c) 2006-2009 Scriptol.com