Attributes
Setter
To have a setter
Set the value of an instance variable
class Student @first_name
@last_name
@username
@passwordenddef first_name=(name)#setter
@first_name = name
enddef to_s
"First name" #{@first_name}"endrumi = Student.new
rumi.first_name = "Rumi"#(= lets you know it's the setter)
#Trying to grab Obj.Rumi(type student class)
#Then first_name method Object of Rumi Obj. & student class method blueprint provides the method for that object.
#Passing in the "Rumi" string the SET the instance variable (@first_name) value to that stringputs rumi
Accessor Method
All I want to do is ACCESS the First Name
class Student@first_name
@last_name
@username
@passwordenddef first_name=(name)#setter
@first_name = name
enddef first_name #Getter -return instance variable
@first_name
end def to_s
"First name" #{@first_name}"endrumi = Student.new
rumi.first_name = "Rumi"
puts rumi.first_name
So, in the last three lines
- I’m creating a new student object
- SETTING the first name to “Rumi”
- Printing out the first name
You can have a last name setter and getter method as well. However, that would be redundant. To do all that work again and again for each instance variable.
Ruby has a way to make things easier for us.
Simply say on top under class
class Student
attr_accessor: :first_name, :last_name, :email, :username
end#takes in these identities & makes them available
#So, then you don't need the two methods already done for the first_nameclass Student
attr_accessor: :first_name, :last_name, :email, :username#def first_name=(name)#setter
#@first_name = name
#end#def first_name #Getter -return instance variable
#@first_name
#enddef to_s
"First name" #{@first_name}"endrumi = Student.new
rumi.first_name = "Rumi"
rumi.last_name = "TheGreat"
rumi.email = "rumi.111@example.com"
rumi.username = "rumi111"puts rumi.first_name
puts rumi.last_name
puts rumi.email
puts rumi.username
So, Getters & Setters are working because they are made available by the ATTRIBUTE ACCESSOR
Say for username we only want to read it.
attr_reader :username
You would need to initialize it
def set_username
@username = "Rumi111"
end
But then you will have to put
rumi.set_username
Initialize Method
makes everything much simpler
class Studentdef initialize(first_name, last_name, username, email, password #takes in all the values you want to set)
@first_name = firstname
@last_name = lastname
@username = username
@email = email
@password = passworddef to_s
"First name" #{@first_name}, Last name: #{@last_name}, username: #{@username}, email address: #{@email}"
endend rumi = Student.new("Rumi", "TheGreat", "rumi111", "rumi111@example.com", "password2")jessica = Student.new("Jess", "Yay", "Jess211", "Jess211@example.com", "password3")puts rumi
puts jessica
rumi.last_name = jess.last_name
#This will give Yay as last name to Rumi
puts "Rumi's last name changed"
puts rumi
This runs first and sets all the values.
You can create two object and made them interact and alter one.
Dealing with user records, could save the last name in the DB.
Conclusion:
Getter & Setter functionality that attribute accessor provides to class.
Key Initialize method that set up our objects when we create them.
How we can create the objects and how we can interact with them.