How to Use Private Member Variables in Ruby [Solved]

How to Use Private Member Variables in Ruby

Ruby-on-RailsIf you’re new to Ruby, especially if you’re coming from a Rails background, you probably use attr_accessor (if not outright adding columns to the table in Rails) to add member variables to a class. Like so:


class Player
attr_accessor :x, :y

What does this mean? It means you can do things like:

p =
p.x = 2;
p.y += 3;
puts "Player is at (#{p.x}, #{p.y})."

i.e. you get the ability to both read and write to x and y, for FREE–without any additional code. Smooth!

But, if you’re like me, you will probably reach a point where you want to add some member variables, but not make them publically accessible. How do you do it?

The answer is to use the @name convention, like so:

class Player
def initialize(x, y)
@x = x
@y = y

Unlike the attr_accessor code above, this does NOT allow you to do things like:

p =, 4)
puts "Player is at #{p.x}, #{p.y}" # raises an error: X and Y don't exist

But, as I mentioned, that’s what this is for–creating private member variables.

Also, realize that in Ruby, objects are very dynamic–you can add properties on the fly! We didn’t explicitly “declare” x and y anywhere in our Player class; yet we can access them.

And that, in a nutshell, is private member variables. One last thing to note–you can almost think of attr_accessor :some_thing as equivalent to:

class Whatever
@some_thing = nil
def some_thing
return @some_thing
def some_thing=(value)
@some_thing = value

But of course, all this is free by using attr_accessor. So use it!


Leave a Reply

Your email address will not be published. Required fields are marked *