Fijxu
August 17, 2024, 5:37pm
1
Take for example, something like this:
class C
include YAML::Serializable
property one : Int32 = 123
property two : String = "property one is: #{one}"
end
This will not work. But if I do something like this:
struct B
property one : Int32 = 123
end
class C
property two : String = "property one is #{B.new.one}"
end
This will work well, but I really wonder if there is “cleaner” way to do something like this.
Something like this?
class C
property one : Int32
property two : String
def initialize
@one = 123
@two = "property one is: #{@one}"
end
end
pp C.new # => #<C:0x74bba8553e80 @one=123, @two="property one is: 123">
1 Like
I think that lazy initialized properties can get you what you want
class User
property name : String
property title : String
property salutation : String { "#{title}. #{name}" }
def initialize(@name : String, @title : String)
end
end
user = User.new("John", "Mr")
pp! user.salutation # => "Mr. John"
user.salutation = "Little John"
pp! user.salutation # => "Little John"
3 Likes