
Originally Posted by
johno
Code:
class Account
attr_reader :balance # accessor method 'balance'
end
You've got to remember that that doesn't create @balance, rather it is more a shortcut for this:
Code:
class Account
def balance
@balance
end
end
So when you do protected :balance it is exactly the same thing which happens when you do protected :some_other_method.
Though this mightbe useful:
Code:
class Account
def balance
@balance / 8
end
def balance= (val)
@balance = val * 8
end
protected :balance, :balance= # and make it protected
def greaterBalanceThan(other)
return balance > other.balance # uses self.balance instead of @blanace
end
end
Douglas
Bookmarks