Hi, can someone explain me what ruby do here?:
class Example
attr_accessor :example_v
def example_v=(example_v)
code
end
end
Ahh, i understand now, thanks very much.
In addition (for anyone else who may read this thread):
The code shown above is how Ruby handles Properties.
A Property is a special kind of class member which generally has two associated methods; a "getter" and a "setter". If the property is read-only, then it only has a getter method. If the property is write-only, then it only has a setter method. Most of the time the getter simply returns the value of an associated private class variable and the setter simply sets this internal variable to a supplied value.
For example, here is a class for a "person" with a read-only "name" and a read-write "age":
class Person
def initialize(personsName, initialAge)
@name = personsName
@age = initialAge
end
def name #getter only
@name #return name
end
def age #getter
@age #return age
end
def age=(value) #setter
@age= value #assign age
end
end
Ruby then allows you to short-cut this code using attributes assigned to identifiers. So the attribute "attr_accessor" equates to writing a getter and setter method for the identified local variable. And the attribute "attr_reader" equates to writing only a getter method for the identified local variable. So the following "person" class is equivalent to the one above:
class Person
attr_reader :name
attr_accessor :age
def initialize(personsName, initialAge)
@name = personsName
@age = initialAge
end
end
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.