这传递了您的示例
class StringWithIndifferentAccess
def initialize obj
@string = obj.to_s
end
def == (other)
@string == other.to_s
end
def include?(other)
@string.include? other.to_s
end
end
更新
所以我只是再次阅读了这个问题,并为所有可以使用 method_missing 并将任何符号转换为字符串的字符串方法“正常工作”,如下所示:
class StringWithIndifferentAccess
def initialize obj
@string = obj.to_s
end
# Seems we have to override the == method because we get it from BasicObject
def == (other)
@string == other.to_s
end
def method_missing(method, *args, &block)
args.map! {|arg| arg.is_a?(Symbol) ? arg.to_s : arg }
if @string.respond_to?(method)
@string.send(method, *args, &block)
else
raise NoMethodError
end
end
end