I'm required" /> I'm required" /> I'm required"/>

Crystal lang - overriding "to string" method

144 Views Asked by At

How to override the "to string" method of an object?

I'm getting the following when printing my object to console:

#<User:0x7fdd9811eb80>

I'm required to have a user-readable version of the object.

1

There are 1 best solutions below

1
R A On BEST ANSWER

Override the #to_s method. For me it was the following within the class definition:

def to_s() : String
    first_name + " " + last_name
end