私は自分のクラスを持っています
class Mutnum
attr_reader :value
def initialize(number)
raise TypeError, "Mutnum created on a non-numeric value" unless number.is_a? Numeric
@value = number
end
def ==(other)
if other.is_a? Mutnum
other.value == @value
else
@value == other
end
end
def set(newval)
@value = newval
end
def to_i
@value
end
def to_s
@value.to_s
end
def +(other)
if other.is_a? Numeric
@value + other
elsif other.is_a? Mutnum
Mutnum.new(@value + other.value)
end
end
def coerce(other)
if other.is_a? Numeric
[other, @value]
else
super
end
end
end
これは基本的に変更可能な数値として機能します (たとえば、1 を関数に渡し、その関数で変更し、呼び出された場所から結果を読み取ることができます)。これを使用して、同じアプリケーションで wxRuby と gosu を使用する際の煩わしさを軽減しています。
のように言えるようになりたいArray#[Mutnum]
、[1,2,3,4][Mutnum.new(3)]
という結果になるはず4
です。
Mutnum を配列インデックス\editとして使用できるようにするには、他にどのような関数を Mutnum editに追加する必要がありますか? と言えると思いますが、さらにデバッグを行う必要があります。[1,2,3,4][Mutnum.new(3).to_i]