11. Why do we need attr_accessor :radians?


#1

For this code, why did we need attr_accessor? Isn't the only time we use radians when we call it at the end? I thought that is why we had the initialize method. I'm not really sure what all the attr type functions do or why they are necessary. Can anyone explain?

class Angle
include Math
attr_accessor :radians

def initialize(radians)
@radians = radians
end

def cosine
cos(@radians)
end
end

acute = Angle.new(1)
acute.cosine


#2

Glad to hear I am not the only one who is struggling with the attr_ objects? methods? modules??
(see what I mean)


#3

attr_accessor creates getter and setter methods for your radians so that it can be get and set from outside the class

@kevin_k_17
methods are like functions but they are called with the context of some object. my_car.unlock_doors would call the unlock_doors method and it would know which car to do it for

modules are for creating namespaces so that when you write code you don't have to worry about what names that other pieces of code are using.

And this topic is OLD X_x