To define a class in Ruby that inherits from Struct
and also has a method, you can use the following syntax:
main.rb81 chars6 lines
In this example, the Phrase
class inherits from Struct
and defines a single attribute :text
. The self.square
method is a class method that calculates the square of a given number.
You can access the text
attribute and the square
method on instances of the Phrase
class:
main.rb112 chars4 lines
This allows you to define a class that has both data attributes and methods.
gistlibby LogSnag