我们有一个应用程序,它使用 Sequel gem 连接到数据源,执行一些工作,然后返回一个结果,该结果具有许多附加到该singleton_class
对象的便利方法。在 ruby 2.3 中,此代码按预期工作:
result = EpulseDB::Employee.where(normalized_args)
result.singleton_class.include(EpulseNormalization)
我们可以看到使用 ruby 2.3.4 的 singleton_class 没有被冻结:
[1] pry(main)> result = EpulseDB::Employee.where(employee_id: 2)
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"employee\" WHERE (\"employee_id\" = 2)">
[2] pry(main)> result.frozen?
=> true
[3] pry(main)> result.singleton_class.frozen?
=> false
[4] pry(main)> result.singleton_class.include(EpulseNormalization)
=> #<Class:#<Sequel::Postgres::Dataset:0x007feff0903660>>
但在 Ruby 2.4.2 中,它似乎singleton_class
被作为冻结返回,我们不能再扩展它。有没有一种新的方法来扩展我应该使用的单例?
[1] pry(main)> result = EpulseDB::Employee.where(employee_id: 2)
=> #<Sequel::Postgres::Dataset: "SELECT * FROM \"employee\" WHERE (\"employee_id\" = 2)">
[2] pry(main)> result.frozen?
=> true
[3] pry(main)> result.singleton_class.frozen?
=> true
[4] pry(main)> result.singleton_class.include(EpulseNormalization)
RuntimeError: can't modify frozen object
from (pry):4:in `append_features'