ruby - String interpolation update after change -
i have values in interpolated string refer array this:
attr_accessor :s, :gamespace def initialize @s = [1,2,3] @gamespace = "#{@s[0]} | #{@s[1]} | #{@s[2]} " end
when change value of @s
, doesn't update value of @gamespace
.
i resorted making additional method this:
def gamespace @gamespace = "#{@s[0]} | #{@s[1]} | #{@s[2]}" end
and call after change @s
.
is there way let attr_accessor
update string interpolation after change without writing method?
a reader method refers instance variable, doesn't re-evaluate it. if want use reader method updated value, thing can not use default setter method, write own.
def s= @s = @gamespace = "#{a[0]} | #{a[1]} | #{a[2]} " end
setting @a
should not done directly, should done through s=
. applies initialize
well:
def initialize; s=([1, 2, 3]) end
Comments
Post a Comment