In the below mentioned program we are defining a variable called paid, while initializing the class object we are creating instance variable default value is false.
We have make_payment instance method defined here and inside that method we assigning value to the paid variable to true, just notice that in this method we are not overriding/replacing the actual instance variable so after calling the make_payment actual instance variable value won't change, if you try to print the paid value of instance will be false.
- class Person
- attr_accessor :paid
- def initialize
- @paid = false
- end
- def make_payment
- puts "making payment..."
- paid = true
- end
- end
- person = Person.new
- person.make_payment
- puts "paid=#{person.paid}"
Try to execute the above code and practice it by changing the variables.
Hope you will understand, i will get back to you with new concept.
No comments:
Post a Comment