To add a public property to a class in Ruby, we can define a getter (to read the value of the property) and a setter (to assign a value to the property). Here is an example code snippet:
main.rb140 chars8 lines
In this example, we define a class MyClass
with a public property my_property
. We use the attr_accessor
method to automatically generate the getter and setter methods for our property.
The initialize
method sets the initial value of the property when an instance of the class is created. We can access the property using dot notation:
main.rb150 chars4 lines
gistlibby LogSnag