Skip to content

Example: Class Attributes

Andrias Meisyal edited this page Aug 29, 2019 · 7 revisions

A class has some attributes. An attribute of a class means data members of an object. It can be a public, private, or protected attribute.

For example, this picture below shows attributes of Author and Book classes.

Class Attributes

If you generate Ruby code with this extension, this extension will create two separated .rb files as well. The generated code of class attributes is shown as follows:

author.rb

class Author
  def initialize(name, email)
    @name = name
    @email = email
  end

  private
    attr_accessor :name, :email

  def to_s
    "Your string representation of the object will be written here."
  end
end

book.rb

class Book
  def initialize(name, price, qty)
    @name = name
    @price = price
    @qty = qty
  end

  private
    attr_accessor :name, :price, :qty

  def to_s
    "Your string representation of the object will be written here."
  end
end

The extension generates attribute accessors, constructor, and a to string instance method, by default. Attribute accessors are grouped by theirs visibility. Class attributes follow the rules on this page.

This extension also supports generating default value of class attributes. You can check the details here.

Clone this wiki locally