Ruby program to implement getter/setter method using ‘attr_accessor’ accessor

We saw a lot of information in the previous article. We are going to know about this very clearly in the article Ruby program to implement getter/setter method using ‘attr_accessor’ accessor. I think you will like this article very much. Let’s go into the article.

Ruby program to implement getter/setter method using 'attr_accessor' accessor

Ruby program to implement getter/setter method using ‘attr_accessor’ accessor

# Ruby program to implement getter/setter method using 
# 'attr_accessor' accessor

class Sample
	#constructor
	def initialize(val)
		@ins_var = val;
	end
	
	#accessor get/set method
    attr_accessor  :ins_var
end

obj = Sample.new("Hello");

val = obj.ins_var;
print "Value is: ",val,"\n";

obj.ins_var = "Hii";
val = obj.ins_var;
print "Value is: ",val,"\n";
Value is: Hello
Value is: Hii

Final Words

Ruby program to implement getter/setter method using ‘attr_accessor’ accessor This article is your favorite topic because it is your favorite. And I will meet you in the next article.

Hi, I'm Selva a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment