Is Class declaration an eyewash in ruby? Is everything really object oriented?

前端 未结 2 1508
广开言路
广开言路 2021-01-22 13:28
class Person
  def name
   puts \"Dave\"
  end
end

puts Person.object_id

There are only two ways of accessing methods :

1) Someclass.method in

相关标签:
2条回答
  • 2021-01-22 13:56

    Yes, classes in Ruby are instances of class Class. In fact, you can create the same class just with:

    Person = Class.new do
      define_method :name do
        puts 'Dave'
      end
    end
    

    Then, you can just type Person.new.name and it will work exactly as your class.

    Checking that Person is an instance of class Class is as easy as typing in your repl Person.class and you get Class in return.

    0 讨论(0)
  • 2021-01-22 14:09

    Yes, Ruby classes are objects:

    >> String.is_a? Object
    => true
    >> String.methods.count
    => 131
    >> Fixnum.methods.count
    => 128
    
    0 讨论(0)
提交回复
热议问题