Q:

Ruby instance variable

class Polygon
  attr_accessor :sides
  @sides = 10
end

When I try to access
puts Polygon.new.sides # => nil

I get nil. How to access sides?
What is wrong here?

A:

Since ruby class definitions are just executable code, when you say @sides = 10 in the context of a class definition, you're defining that on Polygon (not instances of Polygon):
class Polygon
  attr_accessor :sides
  @sides = 10
end

Polygon.instance_variables
# => [:@sides]

You probably want to set the number of sides on the instances of Polygon, from the initializer:
class Polygon
  attr_accessor :sides

  def initialize(sides)
    @sides = sides
  end
end 

Polygon.new(10).sides
# => 10

