crystal-lang

Crystal lang - overriding "to string" method


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.


Solution

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

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