3

I've been studying Ruby on Rails for a class project. I keep hearing "everything in Ruby is an object". What I am not sure I understand is WHY that is a good thing, or maybe IF that is a good thing?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
nikifi
  • 131
  • 2
  • 11

4 Answers4

5

A counterexample would be that in Java Integer is an object but int is not, which means different operations apply to both (admittedly in recent Java there is automatic conversion to/from the object version, but this can introduce unexpected performance issues). Objects are a little slower due to indirection, but more flexible; everything being an object means everything behaves consistently. Again Java would be an example: an array is not an object, and ArrayIterator is something that is bolted on after the fact (with multiple third party implementations, even) and therefore not quite consistent with the way collection class iterators work.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • Another counter-example: in JS `null` and `undefined` are not objects, so when you were expecting an array you can't just say `if (param.length)` when there's the possibility that `param` might be `null`. In Ruby you can modify `nil` to have custom methods (such as `blank?` in Rails, which all objects respond to). – Phrogz Apr 12 '12 at 22:44
  • In Java, arrays *are* objects - they just don't have an awful lot of useful stuff on them, hence all that other stuff that (arguably) should be on the array. –  Apr 12 '12 at 22:49
  • Apparently, [Oracle's planning](http://www.javaworld.com/javaworld/jw-03-2012/120315-oracle-s-java-roadmap.html) to completely remove primitives from the Java language. – Matheus Moreira Apr 13 '12 at 00:52
2

It makes Ruby very flexible. Numbers and other primitive types can be altered or extended.

This can also result in quite elegant syntax:

5.times { print "print this 5 times" }

DanS
  • 17,550
  • 9
  • 53
  • 47
2

Everything in Ruby is not an object (Yes, I know what people generally mean when saying that statement, but its still not totally true). It is more appropriate to say "everything in Ruby evaluates to an object". This is an interesting insight to make, and for the more proper elaboration, I'll simply cite David Black. It's a good read:

http://rubylearning.com/blog/2010/09/27/almost-everything-is-an-object-and-everything-is-almost-an-object/

Pete
  • 17,885
  • 4
  • 32
  • 30
1

Like other people have pointed out already, there are primitives other than objects in other languages like Java. For the compiler and computer, it's a good thing to get the most efficient code, however, programmers need to use different functions and methods based on which one they are dealing with.

Since Ruby is designed for humans, not computers, sacrificing a bit of computational resource for the sake of human's productivity is considered good. Thus, Ruby has never had the distinction between objects and primitives. It definitely lowers the learning curve for novices, too. Internally, Ruby is using a technique called tagged pointers and the performance penalty by the lack of primitives is not as bad, as far as I know.

Another thing worth noting is that in Ruby, classes are also objects, which means you can modify classes and their behaviors with ease even when the code is running. This dynamic nature gives programmers so much power and the Ruby code tends to look quite terse. Ruby on Rails is taking full advantage of this dynamic aspect of the Ruby language.

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
knsmr
  • 281
  • 1
  • 9