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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s