class Portal
property zone_to = 0
property x = 0f32
property y = 0f32
property owner_id = 0
def initialize(@x, @y, @owner_id, @zone_to)
end
def prop_string()
"#{zone_to},#{x},#{y},#{owner_id}"
end
end
p = Portal.new(1f32,1f32,1,1)
pp p.prop_string
However, is it possible to do this dynamically so we don’t have to hard-code the variable in prop_string each time we add a new property to the class?
class Portal
property zone_to = 0
property x = 0f32
property y = 0f32
property owner_id = 0
def initialize(@x, @y, @owner_id, @zone_to)
end
def prop_string()
array_string = Array(String).new
{% for v in @type.instance_vars %}
array_string << {{v}}.to_s
{% end %}
array_string.join(",")
end
end
p = Portal.new(1f32,1f32,1,1)
pp p.prop_string
You can even avoid the runtime join by doing it in the macro. {{@type.instance_vars.map(&.id).join(‘,’)}} will perform the join at compile time and you don’t even have to pay the runtime cost of allocating the string.
That wouldn’t work. That would result in the string being the names of each property joined together, like "zone_to,x,y,owner_id". It has to happen at runtime as it needs to join the runtime values of each ivar.