How would you create getter and setter methods in ruby?
Answer / Renu Paras
In Ruby, you can generate a getter (accessor) method for an instance variable by using the `attr_reader` method. Similarly, you can create a setter (mutator) method using `attr_writer`. If you want both getters and setters, you can use `attr_accessor`.
```ruby
class ExampleClass
attr_accessor :my_var
end
example = ExampleClass.new
example.my_var = 'some value'
puts example.my_var
```
| Is This Answer Correct ? | 0 Yes | 0 No |
Where does a scope change in a ruby program?
Explain the use of retry statement in ruby?
What are the key features of ruby?
What is yield statement in ruby.
How many iterators are there in ruby?
Do you know about dig, float and max?
Explain the difference in scope for these two variables: @@name and @name?
Can method names be capitalized?
What is the difference between a statement and an expression in ruby?
Describe class libraries in Ruby?
Do you know how ruby looks up a method to invoke?
Explain ruby data types.