I get nil, nil but I expected the result to be 9, ‘My Soduko’
class Soduko
attr_accessor :name, :rows, :columns
def initialize
rows = 9
columns = 9
name = 'My Soduko'
end
end
new_soduko= Soduko.new
puts new_soduko.rows
puts new_soduko.name
$ ruby soduko.rb
nil
nil
I thought new would use the initialize method and set those attributes?
What you need is an
instance variablein theinitializemethod. You create an instance variable by prefixing the name with @. When you make a new Soduko object with new_soduko= Soduko.new you want to set instance variables for that particular object. Without the @ you have just created local variables in the initialize method.