Ruby Language

Ruby is a Dynamically Typed, Object Oriented Agile Language, which came to popularity mainly thanks to Ruby On Rails web framework. Ruby was specifically intended as an improvement over Perl Language, but its closest cousin is Python Language. After initial rivalry, it came to be understood that Ruby and Python have complementary design goals. Ruby and Python developers are now supporting each other.

Useful pages:

Hello world in Ruby:'

print "Hello World!\n"

or, using the more common Ruby idiom:

puts "Hello World!"

or, for Cee Plus Plus fans:

$stdout << "Hello World!\n";

or, for Cee Language fans:

printf("Hello World!\n");

or, for those who like their output raw:

p "Hello World!"


Interviews with Yukihiro Matsumoto ('Matz'), Ruby's creator:

An article written by Matz: [1]

An introductory article by the authors of Programming Ruby:

[1] This article is fairly old now, and many of the complaints it has about Python no longer apply.

Help Me: I need a reliable language which lives in ordinary files and has good up-to-date libraries and (most importantly) good up-to-date web frameworks. Obviously Ruby Language is the way to go. But having used Io Language a lot, and also played with Smalltalk Language, Lisp Language and Ioke Language, Ruby Language just seems BORING. Can anyone help me get really excited about it please? -- Jason Grossman

It's like with sex - language is just a tool to achieve the goal. Ruby is top-quality, but you don't want to get too excited about machine tools. Sure, Makino Seiki tool grinder is cool, but do you need it? Can it pay for itself? Do you own a workshop? Do you have a product? Customers? Ruby is comfortable, but don't believe anyone who says it is easy. Achieving full command of its power takes months for geniuses, years for us ordinary people. Unless you are solving a very difficult problem, stick with those languages you already know, or Python Language, which is catching up with Ruby comfort with every new release. It is not the machine beauty, but the problem you are solving that should excite you. If it doesn't, your gut might be telling you that you are trying to do something else than you really want. Search your soul and find a more attractive goal (not necessarily in the realm of computers), and you will love the tools that help you to achieve it. Boris Stitnicky

Not sure I get the comparison with sex, but I think Boris's last three sentences are a profound observation into programming and life. -- David Meyer

Praise for Ruby:

For me, Ruby is the successful combination of Small Talk's conceptual elegance, Python's ease of use and learning, and Perl's pragmatism.

I like it too! It has some kind of elegance, like Eiffel Language, but it has a Perlish flavor nevertheless... I've found the developers and those on ruby-talk pretty supportive and friendly...

One thing I absolutely love: Procs (i.e. code blocks)... -- David Sde Lis (David also wants you to look at Brain Language, .)

With depth of its OO support, Ruby is close to Smalltalk. In Ruby, everything is an executable statement, and up to a small number of exceptions, these are simply method calls. For example, to declare a class with a (read-only) attribute, you could write:

class Example attr :fred end

The difference is, that while language syntax is not user-modifiable, methods can all be redefined by the user, including built-in classes. The 'attr' line might look like syntax, but is actually a method call to a Class object named Example (yup, Ruby has metaclasses). The user can redefine 'attr' like this:

class Class def attr; puts "attr has been redefined!" end end

If you want to, you could redefine #attr to trace all reads or writes to the variable, or perhaps to implement some kind of lazy persistence... Same goes for operators:

+ 3 == 5 || 3 + 4 == 8 # result is, obviously, false

is actually understood as

2.+( 3 ).==( 5 ).||( 3.+( 4 ).==( 8 ) )

where #+, #==, #|| are just method names. Add to this depth facilities such as iterators, closures, mixins, and a clean syntax, and you end up with a language that's practical for building scalable projects, but also fun to play with.

I played with Ruby for a short time and I like it very much. It seems to combine likeable qualities of Perl, Python, and Smalltalk (except for the use of begin/end rather than {/} to delimit block, except procs, where either do/end and {/} is allowed. The Python philosophy is a little too much "There's only one way to do it" for my taste. Ruby combines the cleanliness of Python with the flexibility and concision of Perl and the pure object-orientedness of Smalltalk. -- Dan Schmidt

you can always use braces instead of keywords. --ShawnL

I've written scripts in Perl, Python, and Ruby. I like Ruby best by far. Now if I can get my ISP to put it up on my web site without killing me (after I pushed him for Perl and Python) ... -- Ron Jeffries

use Heroku ( )

Request for SUCCESS real file stories for ruby.... waiting for the miracle... profit... challenge... success... -- Andrey Sidorenko

In fact, Ruby On Rails is the biggest historical success story, that put Ruby over with the audience. But otherwise, check here:

One area where Ruby really shines is in creating domain specific languages. Turns out that Ruby's blocks make this easy to do. I created a Hardware Description Language/simulation environment using Ruby (RHDL) that ended up looking very much like an existing HDL (VHDL). No parser was needed - it's all pure Ruby. This would have been very difficult to do in Python since Python doesn't seem to have anything equivalent to Ruby's blocks. BTW: I was able to implement the first version of RHDL in about 300 lines of Ruby. In addition to Ruby's blocks, the fact that Ruby has continuations also helped a lot. -- Phil Tomson

Ruby requires a lot less boilerplate than many other languages. Commands don't need to terminate with a semicolon; they simply end at endline. (Or the continue to the nextline if you implicitly state that the command's not over, using e.g. a trailing "+", or explicitly using a trailing "\".) Variable names aren't preceded with a $, method call parenthesis can be dropped...

Isn't it easier to make a dumb syntactical mistake, without those restrictions?

Sure it is, but you get used to the quirks soon enough. For more discipline, use Python Language.

Hedging about Ruby:

Maybe I am just not willing to get too bleeding edge, but after looking at the Ruby site (URL at top of this page), I chose not to spend time learning Ruby at this time. I have been experimenting with Python, and have used it for some text manipulation projects with great success. One example is parsing an SNMP MIB and creating a header file with the corresponding MIB variables in it. That way, as Marketing changes the MIB around, I just run a script to recreate the header file. I only have to change my Cpp code if they add or remove variables. Perl seems much more widely used than Python, but Python's readability convinced me to use it. Ruby may be a better language than Python or Perl, but too few people are using it, and the documentation is still too obviously translated from Japanese for me to feel comfortable using it. -- Anonymous Donor

Legacy Pick Axe Book is still worth reading as a historical reference, and legacy Poignant Guide To Ruby has permanent value as a work of art. Those who want to learn, should start at , and continue by simply reading class by class, starting with String class ( ), Array class ( ), and Enumerable module ( ), which defines most of the iterator methods. Ruby syntax 101 can be found eg. here ( ) and the user will also regularly need to consult the operator precedence table, eg. . People who are serious should also get the paper book, such as "Programming Ruby 2.0" ( )

I'll preface this by stating that I actually love Ruby. It's a great scripting language for my needs, and I've been using it quite a bit... enough that I start running into the warty, awkward bits that plague any language. My two "favourites":

Methods don't have lexical scoping, but blocks and Procs do. This little bit of semantic incoherency has caused me a great deal of annoyance -- especially when you really need lexical behaviour, since debugging a stack full of anonymous functions isn't fun.

Here's my impression of the Ruby interpreter, ladies and gentlemen. Ahem...:

$ ruby script.rb script.rb:<last line of file>: syntax error

I swear to God, Ruby has the worst diagnostics of any program I've ever used except ed. It's like the old joke about "Ken Thompson's automobile"... 90% of the time, it won't say anything more useful than "syntax error", and usually it can't even give you the correct line number to start looking at. Oy gevalt!

Phew, that was cathartic.

I've noticed the syntax error at end of file syndrome as well. It usually seems to mean that you started a construct but missed it's corresponding end. I've been reduced to commenting out swathes of code (say, an entire class) in an attempt to track this kind of thing down. Let's say it's an area of the parser which could be improved.

And while I'm on the subject, a couple of my pet peeves:

It annoys me that the parser's definition of 'obviously incomplete' doesn't include a function missing an expected block. Ruby forces you to either place a { as the last character on a line, or to use \ to indicate line continuation (like you're using the C preprocessor). Both are abhorrent. The following seems perfectly reasonable to me, but doesn't compile:

10.times { |i| puts i }

The reason why the above doesn't compile is that Ruby uses {/} also for Hash definition, and the above could mean the user wanting to call 10.times method without block (which is perfectly plausible, since #times is just a method, not a special keyword) and then define a Hash. To Ruby, it looks just lika a mangled hash definition. That's the price you pay for not having to type ; at the end of each line.

I Have This Pattern too, Ruby Language looks very powerful but less usable than Python Language which offers a lot of cognitive support for the programmer. I don't know if it's just my liberal arts background showing through, but code shouldn't hurt to read. -- Larry Price

I'm confused about the statement made here that "code shouldn't hurt to read." I've found that Ruby code is very easy on the eyes, brain, and fingers, compared to Perl or VB. That said, the sample code provided with Ruby does leave room for improvement. I think that the sample biorhythm program would hurt to read in any language, without substantial refactoring. -- David Saff

I'm confused too. I've found that a good coder can write good code even in . Ruby has everything you need to write good, readable code. -- Steve Merrick

Speaking of refactoring - where is the Refactoring Browser for Ruby? I've recently been getting into learning it, but I realized how spoiled I was with the Eclipse Ide's support for Java refactorings. -- Steven Newton

''A Refactoring Browser for Ruby has been written: . It works with Emacs Editor and FreeRIDE. -- pate

While Ruby is nice, I can't help but think that the designer has a bit too much of the "OOP is the light and the way" philosophy in him...

Ruby is not dogmatic about Object Oriented Programming. Ruby is very scalable language and you can pick the appropriate style for each problem.

Check code @ Mame Memo blog:

Strictly speaking, Ruby doesn't have stand-alone functions.

As for not having stand-alone functions, try passing a block into and the object that you get back is close enough for any use that I can think of. -- Ben Tilly

Oh, yeah. And = assigns, while == compares. Duh!

One day I'd like to see a language that uses := for assignment, == for comparison, and leaves = as a syntax error. Ee Language.

My own experience has been that rigid language doesn't prevent me from making mistakes. -- francis

How about : for assignment and = for comparison, like the Self Language? That's one character for each, and I never ever mix them up.

How about making variables objects? ...

Deleted from here to

Ruby variable symbols are preceded by 0-2 at-signs depending on their scope. Local variable is called simply last_word, instance variable @last_word, and class variable @@last_word. I don't know if Matz intended it it this way, but this makes it easier to see dependencies at a glance.

I wonder if ruby-mode in Emacs can be set up colors to distinguish single and double at-signs. (Too bad I'm not proficient in code parsing in Emacs, I'd implement it myself.) -- francis

Have a look at . (Broken Link 2005-05-01) It colours words like TODO and FIXME. You may be able to modify it to colour single and double at-signs. -- Channing Walton

Software implemented in Ruby:

Wikis: -- Japanese-English Wiki Clone. Uses Ruby's RD (POD-like documentation format) as syntax.

Ti Ki -- A wiki with a more conventional syntax.

Instiki -- -- A simple fast Wiki using Textile Format or Mark Down

An RWiki with an inline Ruby interpreter:

Metasploit -- penetration testing software.

YaST2 -- system configuration tool of openSUSE GNU/Linux 13.1 (in part auto-translated from old YaST source code).

Twitter website front end (parts of the Twitter back end are now re-implemented in the Scala language).

Google Sketch Up -- the Ruby interpreter embedded in Sketch Up is used for automated manipulation of 3D objects and for other extensions.

Mini Test : . (now part of the Ruby standard library)

User stories

As a long time Perl user, I love the power of Perl, but have been dissatisfied with the cryptic nature of Perl code. In particular, using objects in Perl is painful. I've been meaning to make the move to Python just to get better OO support, but I was finding it difficult to translate my Perl based knowledge into Python. I discovered Ruby this past summer and found it easy to make the switch from Perl to Ruby. All the power of Perl, with a clean syntax and an OO system that is very reminiscent of Smalltalk. I highly recommend looking into Ruby. -- Jim Weirich

In a bake-off between Java, Python and Ruby for my new Web site project, we selected Ruby and I'm very happy. I love the language syntax - it is short and readable at the same time, matches my intuitive thinking mode even better than Smalltalk (I evaluate a language on that and shortnes of typing [millimemes/keystroke is the unit of measure, but it has to stay readable]. ... That having been said, I detest the @ and @@ variable names as they really clutter up the eye and make the rest of the code harder to read, and the absence of a development environment is . Programming back in the 1980s, (shudder). ... So you might take it as strong evidence of the niceness of the language that we are using it anyway. -- Alistair Cockburn (See The Problem With Sigils about @ and @@)

In summary, this language makes you feel loved. (In remarkable contrast to some other languages.) -- Phl Ip

I personally like to use Ruby too but I've found some quirks that bother me slightly. A few weeks back I was deciding whether to use Python or Ruby and I read both tutorials. In the end (after a long self-debate) I chose for Ruby as it's cleaner, though it lacks the richness in libraries of Python.

Recently, however, I've noticed the quirk I was mentioning: when you access an instance-variable with an attr_reader, you can modify it. (Description of user's encounter with passing by reference followed here.) Now I know that Ruby uses pass-reference-by-value (Like Java) but still, I'd expect an attr_reader to give a cloned object... Just my 2 cents -- Christophe Poucet P.S: I still use Ruby happily

Your problem is trivially solvable by meta-programming, which quickly becomes a habit in Ruby - or at least did for me. I'd do something like:

class Class def attr_clone *args args.each { |arg| class_exec { define_method arg do send( arg ).clone end } } end end

# Later use this newly defined cloning accessor: class Foo attr_clone :myvar end

This really helps cut down on code size a lot. -- Eivind Eklund

I have used both Ruby and Squeak (Smalltalk Language implementation) and I must say that while Ruby is more pragmatic, Squeak is more interesting. The whole environment understands objects, the browser is absolutely fantastic, etc. I think if there was a Squeak-like environment for Ruby, it would be great. Although Ruby is pure OO, Smalltalk is even more. I like that ifTrue:, ifFalse: and ifTrue:ifFalse: are methods of the boolean class.

Ruby's lack of a monolithic graphical IDE will be viewed by some as a feature, not a bug.

I don't see people complaining about ugly Perlisms in Ruby. Though I quite like Ruby, I felt since the beginning that a lot of fluff needs to be thrown out. All of those $-initial variables (especially $_, aaargh), and the statement modifiers.

try require "English" --Shawn Landden

"Come to think of it, does Ruby have higher order functions?"

Ruby has a convention for higher order function emulation, yes. Sadly, it's not unified with the block mechanism, which aims at partly the same solutions. BTW: Can you write anonymous "functions" like these (objects with a call method) in Ruby? (Answer: Yes) And less related, can an iterator (or whatever a method receiving a block is called) pass the block to a subroutine? (Answer: Yes)

Final remarks and references

Bow before Ruby Forge.

Learn to use Ruby Gems.

Use Bundler to create your own gems.

Ruby interfaces to other programming tools

A useful feature which helps with this is Ruby Coerce.

Call inline C (Ruby Inline Gem)

Call from Cee Plus Plus to Ruby with callbacks using Ruby Embed.

Learn to use Gtk Two Gem (gem install gtk2, requires gtk libraries).

There are several ways to form a Ruby GUI. Try eg. Rugui Gem.

Interprocess Communication using Ruby

Logging from Ruby

Embed Ruby in a web page

Ruby Books

Large list of Ruby books:

Ruby Patterns

Design Patterns In Ruby (book see above)

Pattern Implementations (on the same wiki)

See original on