Everything in Ruby is an Object
It was mentioned in the last section that Ruby took the best of Object Oriented Programming from Smalltalk. In Ruby, as in Smalltalk, everything is an object.
Let me demonstrate this:irb(main):001:0> nil.class=> NilClassirb(main):002:0> 1.class=> Fixnumirb(main):003:0> 1.2.class=> Floatirb(main):004:0> "something".class=> String复制代码In Ruby, nil is an instance of NilClass, 1 is an instance of Fixnum, 1.2 is an instance of Float, and so on.
In programming languages like C, you had a fixed set of data types, and a fixed set of things they could do. But in a language like Ruby where everything is an object, you can create new data types or even change the existing ones. This gives you great power, but as Uncle Ben from Spiderman tells you:
With great power, comes great responsibility
which brings me to my next section.
|