Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class methods syntax in ruby

Tags:

syntax

ruby

the ruby book I'm reading has confused me a bit. If I do the following, I understand completely why the code throws an error;

class Person
  def show_name
    puts @name
  end
end

person = Person.new
person.show_name
Person.show_name   #(note the capital P) this line falls over

It throws an error stating that the Person class does not have a method called show_name, because it is an instance method. I understand this completely. The book then throws in this example;

class Class
  def add_accessor(accessor_name)
    self.class_eval %Q{attr_accessor :#{accessor_name}}
  end
end

class Person
end

person = Person.new
Person.add_accessor :name    #note the capital P
Person.add_accessor :age     #capital P here too

person.name = "Mikey"
person.age = 30

puts person.name

and goes on to state how cool it is that you can add methods to classes dynamically. What I don't understand is why I am suddenly allowed to call the "add_accessor" method as a class method (with a capital P) when the method itself isn't defined as such? I thought all class methods had to be declared like this?

class Math
  def self.PI
    3.141
  end
end

puts Math.PI

Is anyone able to enlighten me?

like image 903
Mikey Hogarth Avatar asked Jul 23 '26 12:07

Mikey Hogarth


1 Answers

Ruby classes are objects like everything else to. Your Person class is really an object of class Class, which in turn inherits from class Module. When you add a method to class Class as an instance method, you are providing a new method for all classes. If you had declared it with a def in Person, it would not be callable without an object. To add class methods for one class, but not all, you must prepend the method name with self or the name of the class:

class Person
    def instance_method
    end
    def self.class_method
    end
    def Person.other_class_method
    end
end

When you declare the method as self.class_method you are declaring your method to be a singleton method on the class object. self in a class or module declaration refers to the class object, which is why self. and Person. are the same. When dealing with Ruby, just remember everything is an object, everything has methods. There are no functions in Ruby either, despite appearances to the contrary. Methods and objects, always.

like image 93
Linuxios Avatar answered Jul 26 '26 02:07

Linuxios



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!