2

String、Fixnum、または Float のいずれかに強制できるオブジェクトを定義する方法はありますか? これは、値を収集し、それらを使用して単純な式の制限されたセットを評価するシステムで使用するためのものです。

私は試した:

class EmptyValue < Numeric
  def to_s; ''; end
  def to_str; ''; end
  def to_i; 0; end
  def to_int; 0; end
  def to_f; 0.0; end
end

しかし、これは失敗します

1 + e
TypeError: EmptyValue can't be coerced into Fixnum
4

2 に答える 2