So far, my favorite programming language has been python, hands down, but I've been hearing a number of good things about ruby from various people I hold in good regard, so let me put it to you:

  • Why should I learn Ruby?

Please do not post something to the effect of "Because it is awesome" or "It's better than language X because..." I don't want a war on my hands. I'm looking for concrete examples of what makes Ruby a useful language to code in.

+2  A: 

Ruby is a very elegant and powerful language with a strong community behind it. I know I switched from Python to Ruby a while ago because of the elegance of Ruby, and not for any particular practical reason, but I've never regretted it. If you give examples of the sort of work you do in Python, it might be easier to show why Ruby's a better fit (or maybe why it isn't).

Kevin Ballard
+2  A: 

For me, it has a very consistent object model. EVERYTHING is an object, including nil. In general, if a method (such as each) exists that does something... similar methods in other classes will be named the same.

I would say learn it if you like learning new languages anyway, then you can pick for yourself if you prefer Ruby or Python. As long as you have some varied languages under your belt, it doesn't matter.

Mike Stone
+3  A: 

From a purely academic perspective, learning Ruby might be useful because it provides you a different approach to how particular problems could be solved. Something that might be done "the one true way" in python could be done a totally different way in Ruby - which might in turn provide a method of improving your Python code.

This reason is the same reason you should learn as many programming languages and paradigms as you can.

Matthew Schinckel

Coming from a PHP background I've been really pleased with how succinct it is, I like that there aren't a hodge-podge of functions. I also really like the strong object model and the flexibility you get with open classes. I think it's an elegant language and allows you to write code that is very human readable, even by non-programmer types. Plus, it's not especially hard to learn and the idioms are easy to pick up.



I was more looking for specific examples of why you feel Ruby is an elegant and powerful language. Do you have a code snippet that you feel accentuates some nice feature?

The reason for asking this was twofold: First, I am considering learning some Ruby and was curious what specifically about it made it a good language; and second, I was feeling that stackoverflow needed a decent thread to this effect for every language being discussed on the site, this way people who haven't used the given language can get a taste of what it's all about.

+6  A: 

If you already know python, I'm not sure you should learn ruby.

If you don't already know a more enterprise-y language like Java or C#, I'd learn that instead. You already know a very good and powerful loosely typed language which is ideal for all the same things ruby is. Better learning something more different to not only improve your skill set, but also learn from a wider breadth of tools.

Karl Seguin
+5  A: 

Oh yeah, go to try ruby website. It gives a very easy, nice and quick interactive tutorial within the browser. After I went through that, I was hooked. It should give you an idea of if you want to continue with Ruby or not.

Mike Stone
+4  A: 

The biggest reason to switch is the block support, both in syntax and in the standard library. It's a great bit of shorthand for passing a closure into a method. You may take a bit of this functional programming style with you back in Python land as well.

From a purely technical standpoint, Ruby and Python are really quite similar. Their differences are social and philosophical.

You may or may not like the DSL style that many Ruby libraries use. I find that they're great for surface-level usability (especially when there's good documentation), but it creates a bit of a road block when you need to understand exactly what's going on.

If you're looking to broaden your technical skills, and you know Python backwards and forwards, then you might get more out of a more distinct language. Good candidates include Smalltalk, O'Caml, Haskell, and Erlang.

Peter Burns
+15  A: 


As Mike said, in ruby everything is an object, unlike Python where the primitives are, well, primitive. Another really elegant aspect of Ruby is anonymous code blocks. They're used all over the place, especially to replace loops that you see in other languages.

For example, if you want to repeat something 15 times, you can write:

15.times do
# do something here

Notice that you can actually read this like it's english.

Or if you want to loop over an array, you can do:

%w{one two three}.each do |word|
# this code is repeated with word = "one", then "two", then "three"

Naturally it supports map and reduce (called inject) as well:

# sum the absolute values of every number in the array
nums.map { |x| x.abs }.inject(0) { |acc,i| acc + i }

If you use Ruby on Rails, or Ruby 1.9, this can be simplified even further:

nums.map(&:abs).inject(0, &:+)

In this case the &:abs sequence is the same as :abs.to_proc which returns an anonymous code block that calls the abs function on its first argument. And &:+ returns an anonymous code block that calls + on its first argument, passing in the second argument to the + method.

Another common thing to do in Ruby is to write a DSL. One of the more popular ones is used by Rake, a make replacement for Ruby. A simple Rakefile might look like this:

desc "Build and run"
task :run => [:build, :launch]

desc "Build the project"
task :build do
sh "xcodebuild -configuration Release build OBJROOT=build/ SYMROOT=build/"

desc "Clean the project"
task :clean do
sh "xcodebuild clean OBJROOT=build/ SYMROOT=build/"

task :launch do
sh "build/MyProject.app/Contents/MacOS/MyProject"
Kevin Ballard
"nums.map { |x| x.abs }.inject(0) { |acc,i| acc + i }" You can't quite read this bit as english though...
Actually, you're totally wrong about python. Python doesn't have primitives at all. You might be one of the confused ruby fans misquoting some old python documentation, but rest assured that everything in python is an object, and has been since at least 2.0.
Aaron Gallagher
+1  A: 

I'm interested in this question also since I've been thinking of learning Ruby lately.

However, I can't agree with Eridius' answer. First, numbers and strings are objects in Python also (Even None):

>>> (0).__class__
<type 'int'>

>>> "".__class__
<type 'str'>

>>> None.__class__
<type 'NoneType'>

The other examples shown (lopping and map/reduce) I think are more easily expressed in Python as list comprehensions, and DSLs are also easily written in Python also.

I was expecting something more on the lines of Peter Burns' answer. Block support is a feature that's really different from what can be done in Python. Any other features in Ruby that aren't/harder in Python?


Julio César

I don't have time to cook up a concrete example, but most of the time, Ruby lets you do things in a clean, concise and compact way, compared to other languages I've used (and I've used plenty).

Ruby is a language which heavily uses DRY and the Principle of the Least Surprise, which both help keep the language saner than any other.

And most of all, it lets you get things done quickly without making you feel a dirty scriptkiddie.

+1  A: 

Ruby, like many have mentioned, is completely object oriented... everything is an object, and it could be interesting from that perspective.

However, the direct answer to your question is likely "Because it's the hot thing for web startups, mostly because Rails is a very convenient framework for building Web 2.0-y things, and will give you the opportunity to work in one of these startups." Beyond that specific business/personal requirement, I really feel like the only reason to learn Ruby is curiousity.

One thing that Ruby really suffers from is the lack of a great IDE. I've tried them all... multiple times. JEdit, Netbeans, Aptana, Sapphire in Steel (the VS plug-in), Arachon - they all pale in comparison to the combination of C# + Visual Studio. And I know part of this is due to the fact that Ruby is a dynamic language and all, but it's just no comparison.

+11  A: 

You should stick with python.

Ruby libraries try too hard to look like natural language. This may seem good at first, since it sometimes makes it easier to read the code's intent. However, it soon feels like you're learning a new language every time you want to use another ruby library. You can't take anything for granted. Suddenly you can't make use of any familiar tools, and you can't even find clever ways to use someone's quirky library -- you have to do it the way they designed it. It defeats my programmer spirit. Take this bit of rspec for example:

# What sort of object does at\_least(int) return that only times() can act on?

As you create bigger projects, it becomes increasingly important to be explicit and use namespaces to your benefit. Ruby developers tend to pretend namespaces don't exist, and will use mixins all over that can break things. Modules are rare, and piece-wise import does not exist. Also, there is no way to tell at a glance if a method you're calling is part of the same scope, same class, a parent class, or even global -- they all look the same, and can clobber each other since ruby does not support shadowing.

As for Ruby Blocks, python introduced those in 2.5. They're called generators, which are just regular functions that contain the yield statement, and act like iterators. This implementation is even more useful than ruby's.

To ruby's benefit, its standard library is much more consistent with object oriented languages, while much of pythons standard library is a thin wrapper on C. Some standard functions were designed backwards, like len(sequence) instead of sequence.length and ", ".join(sequence) instead of sequence.join(", "). I think this is why some people say python is 'not as object oriented', even though technically it is. But don't get me started on python's time/datetime modules.

Also, there are some really neat ruby libraries, like Shoes, which may be the only reason you need to start learning =]

In conclusion, I would not say it's a bad idea to learn ruby. It can be a lot of fun. In fact, I used to prefer it. But now that I've used the two for a few years, I find python to be a lot more practical for larger projects. I've come to agree with import this.

Nick Retallack
+2  A: 

You shouldn't. Ruby and Python are too similar. Learning one when you already know the other will not gain you any significant insight.

Instead, try to learn languages which are really different, like say Prolog, Haskell, Io, Scheme and Erlang.

Jörg W Mittag
+1 for Haskell, it is really awesome