Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想让其他模块/类/文件可以使用类扩展。例如:
module UsefulStuff class Object def blank? respond_to?(:empty?) ? empty? : !self end end end
在其他类/模块/文件中:
if string.blank? ...
什么以及如何包含/加载/要求/... 使这项工作?
您应该使用模块而不是类:
module UsefulStuff module Blank def blank? respond_to?(:empty?) ? empty? : !self end end end
您可以将其包含在 String 类中:
class String include UsefulStuff::Blank end
或者,如果您希望它对对象是全局的:
class Object include UsefulStuff::Blank end