Expressions @ Programming Rubyで述べたように、
引数 (任意の式) が定義されていない場合、defined?
演算子は戻ります。定義されていない場合は、その引数の説明を返します。nil
defined? 1 → "expression"
defined? dummy → nil
defined? printf → "method"
defined? String → "constant"
defined? $& → nil
defined? $_ → "global-variable"
defined? Math::PI → "constant"
defined? ( c,d = 1,2 ) → "assignment"
defined? 42.abs → "method"
クラスの場合:
ruby-1.9.2-p180 > defined? Person
=> nil
ruby-1.9.2-p180 > class Person
ruby-1.9.2-p180 ?> end
=> nil
ruby-1.9.2-p180 > defined? Person
=> "constant"
メソッドの場合:
ruby-1.9.2-p180 > def cool
ruby-1.9.2-p180 ?> puts "clear"
ruby-1.9.2-p180 ?> end
=> nil
ruby-1.9.2-p180 > defined? cool
=> "method"
あなたの場合、
defined?(options)
options メソッドを呼び出して、結果を に渡していますdefined?
。
@Stuart のおかげで、実際のメソッドではなく、method_missingによって実装されていると述べたように、ここには少し魔法があります。
したがって、実際には、メソッドのオプションが定義されることを期待してはいけませんbaz
。