In the latest versions of Ruby (2.5 and above), you can use a shorter syntax to define classes. This syntax is known as "class definition with keyword arguments". Here's an example:
main.rb189 chars13 lines
With this syntax, you can define the attributes of your class using keyword arguments in the initialize
method. The attr_accessor
macro is used to define getter and setter methods for the attributes.
You can create an instance of this class and call its methods like this:
main.rb63 chars3 lines
This will output:
main.rb35 chars2 lines
This shorter syntax provides a more concise and readable way to define classes in Ruby.
gistlibby LogSnag