I don't know of any newbie guides, but I can explain the getter/setter stuff.
Instance variables are private by default in Ruby, so all you have to do to make the class in your example equivalent to the Java class is remove the attr_accessor line.
attr_accessor, along with attr_reader and attr_writer are shortcuts for the simplest possible getter and setter methods:
Code:
attr_reader :name
# (read public) equivalent to =>
# def name
# return @name
# end
attr_writer :address
# (write public) equivalent to =>
# def address=(address)
# @address = address
# end
attr_accessor :telephone
# (read and write public) equivalent to a combination of attr_reader
# and attr_writer
If you want to provide your own getter or setter to provide additional functionality or to make virtual attributes or whatever, simply take out the shortcut, or change it to provide only the other type of accessor, and write your own:
Code:
# Methods that end in = are lvalue eligible, so you can do
# class_instance.telephone = 1231234
# to call this
def telephone=(telephone)
raise Exception.new('Yeah right') if telephone == 5551212
@telephone = telephone
end
# allow classinstance.address
def address
return @address.upcase
end