Ruby craziness: Class vs Object?

后端 未结 7 1825
陌清茗
陌清茗 2020-12-07 08:53

I just started playing with JRuby. This is my first ruby post. I had a hard time understanding classes vs objects in Ruby. It doesnt mean like what classes & objects in

7条回答
  •  生来不讨喜
    2020-12-07 09:23

    Ramesh, in ruby everything is an object, and a Class is no exception.

    try this in irb

    ruby-1.9.2-p136 :001 > o = Object.new
    => # 
    ruby-1.9.2-p136 :002 > o.is_a? Class
    => false 
    ruby-1.9.2-p136 :003 > o.is_a? Object
    => true 
    

    in this case, I've created an instance of an Object, and checked if it's a class (false) or a Object (true).

    A Class in ruby, is some kind of template object used to create instances of that class. Sorry that this is not super clear. The key concept is that ruby is a pure object oriented language, as opposed to Java.

提交回复
热议问题