[ next ] [ prev ] [ contents ] Invitation To Ruby

Attribute Reader

The following classes are equivalent.

Top can be explicitly written as a method ...

  1: class Stack
  2:   def initialize
  3:     @items = Array.new
  4:     @depth = 0
  5:   end
  6:   def depth
  7:     @depth
  8:   end
  9:   # ... other methods
 10: end

Or top can be implicitly defined by attr_reader

  1: class Stack
  2:   def initialize
  3:     @items = Array.new
  4:     @depth = 0
  5:   end
  6:   attr_reader :depth
  7:   # ... other methods
  8: end



[ next ] [ prev ] [ contents ] Copyright 2002 by Jim Weirich.
All rights reserved.