Attr_accessor In Its Natural Habitat

And here we see the elusive Attr_accessor in it’s natural habitat, just above the initialize method.   

class Card
attr_accessor :rank, :suit

def initialize(rank, suit)
@rank, @suit = rank, suit
end

end

Watch how gracefully it executes a reader and writer method in a single line, making the following code unnecessary:

def  rank                              #reader method

@rank

end

def  rank=rank                     #writer method

@rank =rank

end

She truly is a beautiful beast.

Advertisements