Skip to content
Advertisement

Why am I able to call the class method as if it were an instance method here?

I was looking through this example:

class SQLObject
  def self.columns
    return @columns if @columns
    columns = DBConnection.execute2(<<-SQL).first
      SELECT
        "#{table_name}".*
      FROM
        "#{table_name}"
      LIMIT
        0
    SQL
    columns.map!(&:to_sym)
    @columns = columns
  end

  def self.table_name
    @table_name ||= self.name.underscore.pluralize
  end

  def insert
    column_symbols = self.class.columns.drop(1)
    column_names = column_symbols.map(&:to_s).join(", ")
    question_marks = (['?'] * column_symbols.count).join(", ")
    DBConnection.execute(<<-SQL, *attribute_values)
      INSERT INTO
        #{self.class.table_name} (#{column_names})
      VALUES
        (#{question_marks})
    SQL
    self.id = DBConnection.last_insert_row_id
  end
end

And I was confused as to why it’s ok to call the table_name method in the “self.columns” method as if it were an instance method. Isn’t the “table_name” method a class method? Hence, shouldn’t it be called as “self.class.table_name” in the “self.columns” method as well?

Advertisement

Answer

When inside an abstract class, the self refers to the proper class, not the object. That’s why you can access the method without explicitly telling self

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement