Define A Method On A Struct
When defining a Ruby Struct
, you can optionally pass it a block that defines any number of methods as instance methods on that Struct
. They can reference the attributes of the Struct
.
Here is an example of a person Struct
with a full_name
method that uses the first_name
and last_name
attributes.
This Struct
can be used like so:
This is a great way to make a Struct
just a bit more powerful without having to convert it into a full-blown PORO (plain old Ruby object).
Last updated