Ruby Class Members: Getter & Setter

The simplest example of read_attr, write_attr in a class.

class Person  
  attr_reader :name, :age   
  attr_writer :name, :age   # creates the setter methods       
  def initialize(name)  
    [@name]( = name  
endmike ='Mike')   
mike.age = 20               # calling setter method  
mike.age                    # calling getter method, returns 20


The example is verbatim from an easy to follow tutorial on Ruby class:


Knowledge is the seed of wisdom.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.