Thursday, March 15, 2007

Attribute

In Ruby terminology, properties of objects that you can set and/or get are called attributes.
In the case of ticket objects, we would say that each ticket has a price attribute as well as a date attribute and a venue attribute.

Ruby has a shortcut lets us create that style of method: a method that reads and returns the value of the instance variable with the same name as the method. We do it like this:

class Ticket
      attr_reader :venue, :date
      attr_writer :studio
      attr_accessor :price
end

attr_reader是只读的attribute;
attr_writer是只写的attribute;
attr_accessor等于attr_readerattr_writer

No comments: