I have been reading a lot about Ruby the past few days. Every SO post I come across I hear that ruby is an elegant language. Can you guys give an example of why ruby is elegant compared another language?
-
Similar: http://stackoverflow.com/questions/1589841/what-does-it-mean-when-someone-says-that-perl-is-more-expressive-lanaguage – Josh Lee Nov 28 '09 at 13:39
8 Answers
It's considered elegant because it's orthogonal. That's a fancy way of saying that similar operations apply to similar operands.
Simple example: +
on integers adds them; on floating point numbers, ditto. On big integers, too. On strings, it concatenates them (which you'd kind of expect too). Now this is not a big deal for +
, you kind of expect it from any decent programming language. But there are operations like map
or filter
, they work on lists (they should!) but they also work on arrays and in fact on anything that can be enumerated or iterated through.
I like how array (or list) indexing works, you can use positive integer indexes to index from the start, or negative indexes to specify a position back from the end of the structure, you can specify a range to pull out a subset... this works for lists, arrays and (sub)strings too. It works on the right side of an assignment (=
), it works on the left side too (you can assign to a substring, thus replacing part of a string). So you don't need a substring_replace
function, you just leverage an existing, general concept.
The author of Ruby expressed this in terms of satisfying the user's (i.e. the programmer's) expectations: There should be as few surprises as possible, whenever common sense would lead you to expect that something works in a certain way, it simply should. He worked very hard to satisfy this requirement. Also, while Ruby borrows a little bit from Perl, the author disagrees with Perl's TMTOWTDI principle in favor of the Zen of Python: "There should be one –and preferably only one– obvious way to do it."
It's also nice that Ruby does closures (= code blocks) so you can specify a function simply by wrapping it in a pair of braces. There are places where it's appropriate to specify a function inline, and Ruby lets you do it conveniently.
Ruby lets you do things with a small amount of coding because its constructs fit together in powerful ways. I dabble in Project Euler and I find that often the shortest legible and understandable solutions were done in Ruby. The very shortest are in J, but that's an APL dialect and to the uninitiated it looks like line noise.
My personal experience bears this out: I taught myself Ruby and Rails and wrote a Web application with medium-complex data analysis in one week. Every principle I learned, I could apply in different places with different data – It Just Works™!

- 66,391
- 18
- 125
- 167
-
DISCLAIMER: I could be fuzzy on details in my above answer, as I'm not actively using Ruby at the moment. If I got something wrong, let me know and I'll fix. I loved Ruby some years ago for said elegance but was turned off because the reference implementation was dog slow. Meanwhile there's JRuby, about as fast as Java, and I'd consider that for my next project. – Carl Smotricz Nov 28 '09 at 12:16
-
4Orthogonal in a computing context means free of side effects, so for example, modifying one component doesn't affect dependent components. – John Topley Nov 28 '09 at 12:18
-
1From Wikipedia: "An instruction set is said to be orthogonal if any instruction can use any register in any addressing mode." - this is the connotation I was basing my discussion on. I agree that this isn't totally clear-cut, which is why I had to follow up with a lot of hand-waving discourse. – Carl Smotricz Nov 28 '09 at 12:27
-
+1 for the "line noise". There are a few languages like that and the practitioners of them always seem puzzled that everyone else likes readable code. – Donal Fellows Jul 13 '10 at 10:29
-
On the contrary, Ruby is about "more than one way to do things: https://ruby-talk.ruby-lang.narkive.com/OyMLikAo/ruby-s-more-than-one-way-to-do-things#post2 – karatedog May 05 '21 at 22:42
- having open classes i.e. you can add methods to classes after they're defined
- having method_missing i.e. the possibility of handling cases where you're send a message you haven't defined a method for. Again this allows you to write code that adapts rather than just crashing.
- having a Smalltalk-like, consistent OO model e.g. you can do things like 1.class 1.times {}. This makes a lot of the DSL support possible.
- it has blocks/closures - makes it a lot easier to write flexible code
- it doesn't waste your time with static typing of each variable (i.e. trying to solve problems that aren't important if you do TDD)
- You don't have to use class-based OO i.e. it supports prototype-based OO programming

- 6,941
- 3
- 39
- 48
In my experience blocks are the biggest contributing factor to making Ruby elegant. what's more elegant than writing each to iterate over arrays/hashes/etc...
arr = ["one", "two", "three"]
arr.each { |e|
puts e
}
However I believe it's more than this, the elegance of the Ruby language also comes from the libraries. Most libraries have kept to using the unique Ruby 'style' for function names, such as 'each' for iteration or the usage of '!' and '?' at the end of function names for destructive/boolean returning functions, this is what really keeps Ruby 'elegant'.

- 2,245
- 4
- 19
- 24
Ruby is all about productivity and programming fun, however there are several reasons why Ruby is elegant:
- Pure OOP: Everything is an object, so you don't have to distinguish between primitive and object types.
- It supports both Functional and Imperative programming paradigms: that leads to both concise and readable code.
- It supports the Principle of Least Surprise: which supports readability.
- It has closures and blocks: which is really cool for doing Internal DSLs.
- It has a robust Metaprogramming API: which empowers writing Internal DSLs as well.
- It's very good at scripting tasks: handling text and xml files and doing administration related stuff(it's being used widely by system admins now a days).
- It has a very cool and supportive community: you will find support and help whatever your problem is. This community supports modern and fashional coding techniques as well like TDD and BDD.
The sum of all the above is that using Ruby you can have:
- Concise code: Compared to Java and C++, at least you can save yourself 50% of code typing.
- Readablility.
Thus having readable and concise code -> less code to write -> less code to test and maintain -> Productivity.
I shouldn't forget to mention the great supportive community behind Ruby.

- 23,590
- 15
- 91
- 109
I may not be able to argue that it's elegant, I think that in general elegance is derived by the programmer's implementation. However I can argue that it is concise and I think that's what a lot of people are really feeling when they say that Ruby is elegant. Often times code that comes together quickly feels elegant.
You can see the results of the programming language shootout here. You'll notice that Ruby 1.9 is smacked flat against the left side meaning it's super concise. I would wager that anyone talking about a language being elegant is talking about a language that is either on the left side or close. Haskell being one of the only notable exceptions I know which takes a lot of effort to get certain things done but still feels exceedingly elegant doing it.

- 4,560
- 24
- 31
-
1If you want to focus just on Code-used you set your own parameters for a ranking like this - http://shootout.alioth.debian.org/u32/fun.php?d=data&calc=calculate&xfullcpu=0&xmem=0&xloc=1&binarytrees=1&chameneosredux=1&fannkuch=1&fasta=1&knucleotide=1&mandelbrot=1&meteor=0&nbody=1&pidigits=1®exdna=1&revcomp=1&spectralnorm=1&threadring=0 – igouy Dec 01 '09 at 16:01
everthing is an object (ps. like in smalltalk ..):
3.times { p "olleh" }
extensible/open classes (e.g. from Rails):
10.days_ago
.. and more on ruby elegance: http://www.benhughes.name/files/presentations/ruby_elegance.pdf

- 181,842
- 47
- 306
- 310
-
-
2
-
-
-
Hmm... the fact that numbers are objects and have methods like other objects is part of what makes Ruby elegant. Looking at this answer, I figured out that this is what The MYYN wants to tell us. But it was a bit daring to expect his readers to make that intellectual leap based on just those two examples! :) – Carl Smotricz Nov 28 '09 at 13:37
-
It probably only feels like a leap if you don't know Ruby. Rubyists often forget that certain things are not obvious to everyone. One potential other example might be: 3.class #=> FixNum. I was actually wondering myself if it really is an object and it turns out it is. I feel like Ruby has some primitives though, I just don't remember where to find them :P – Chuck Vose Nov 28 '09 at 17:25
-
Everything being an object makes Ruby super slow compared to Python, but it's consistent. – karatedog May 05 '21 at 22:46
Just an amusing side note: this feels like php yet is Ruby (from earlier today), but the first answer feels like elegant Ruby. In PHP I ended up writing stupid long code like the linked post to do little things. In part because lambdas are basically nonexistent. So I would have to say that Ruby's lambda support together with map/reduce is what makes it elegant to me.

- 1
- 1

- 4,560
- 24
- 31