🖥
Ruby | メソッドを最小限しか持たないクラス ( ブランクスレート ) を作る
BasicObjectを継承するだけで良い。
class Blank < BasicObject
end
最小限のインスタンスメソッドだけを持った状態になる。
Blank.instance_methods
# => [:!, :==, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]
継承ツリーの上にはBasicObjectだけが置かれる。
Blank.ancestors
# => [Blank, BasicObject]
解説
継承クラスを指定しなかった場合は Object
からの継承になる。
class NotBlank
end
NotBlank.instance_methods
# => [:instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :is_a?, :extend, :define_singleton_method, :to_enum, :enum_for, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :inspect, :display, :send, :object_id, :to_s, :method, :public_method, :singleton_method, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :==, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]
NotBlank.ancestors
# => [NotBlank, Object, Kernel, BasicObject]
環境
- Ruby 2.3.1
参考
- メタプログラミングRuby 第二版
チャットメンバー募集
何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。
公開日時
2017-03-18
Discussion