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?
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
- 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
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.
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'.
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.
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.