nikifi
nikifi

Reputation: 131

Is it good that "everything is an object"?

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?

Upvotes: 3

Views: 705

Answers (4)

knsmr
knsmr

Reputation: 281

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.

Upvotes: 1

Pete
Pete

Reputation: 18075

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/

Upvotes: 2

DanS
DanS

Reputation: 18463

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" }

Upvotes: 2

geekosaur
geekosaur

Reputation: 61439

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.

Upvotes: 5

Related Questions