views:

697

answers:

9

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?

+1  A: 

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

The MYYN
Hmmm..I wonder what this peace of code does?
Luke101
therefore smalltalk is elegant too?
klez
@Luke101: It prints "olleh" 3 times.
sepp2k
This is elegant? Is that spelt "Opaque" or "Obtuse"?
gbn
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
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
A: 

It is no more elegant than the next language.

Conrad Meyer
Agree... but then I think SQL is elegant ;-)
gbn
If the next language were, say, PHP then one might perhaps consider the possibility of disagreeing.
Mike Woodhouse
One can argue that elegance is subjective, and as such reflects what one brings to the table. However, in many areas (art, architecture, music, programming languages), there is generally a consensus on the existence of Elegant Design. Languages _definitely_ differ in that sense. One would be hard-pressed to favour COBOL over Smalltalk, for example.
Michael Easter
+3  A: 

Ruby and DSLs has been discussed a lot.

Example from sinatra:

get '/' do
    'Hello world!'
end

Or from this blog:

ChessGame.new do |move|
  move.black_pawn(forward)
  move.white_pawn(forward)
  #…
  move.white_queen(pwn_king)
end
The MYYN
+11  A: 
Carl Smotricz
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
Orthogonal in a computing context means free of side effects, so for example, modifying one component doesn't affect dependent components.
John Topley
From 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
+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
+4  A: 
  • 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
cartoonfox
+2  A: 

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.

Chuck Vose
igouy
+3  A: 

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

Jamie
+2  A: 

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.

khelll
A: 

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.

Chuck Vose