ライブラリ
- ビルトイン (32)
キーワード
- < (3)
- <=> (3)
- ancestors (3)
-
append
_ features (3) -
const
_ defined? (3) -
const
_ get (3) - constants (2)
- include? (3)
- included (3)
-
included
_ modules (3)
検索結果
先頭5件
-
Module
# include(*mod) -> self (18168) -
モジュール mod をインクルードします。
...ます。
@param mod Module のインスタンス( Enumerable など)を指定します。
@raise ArgumentError 継承関係が循環してしまうような include を行った場合に発生します。
module M
end
module M2
include M
end
module M
include M2
end
実行結......果:
-:3:in `append_features': cyclic include detected (ArgumentError)
from -:3:in `include'
from -:3
インクルードとは、指定されたモジュールの定義
(メソッド、定数、クラス変数) を引き継ぐことです。
インクルードは多重継承......の代わりに用いられており、 mix-in とも呼びます。
class C
include FileTest
include Math
end
p C.ancestors
# => [C, Math, FileTest, Object, Kernel]
モジュールの機能追加は、クラスの継承関係の間にそのモジュールが挿入
されるこ......果:
-:3:in `append_features': cyclic include detected (ArgumentError)
from -:3:in `include'
from -:3
インクルードとは、指定されたモジュールの定義
(メソッド、定数) を引き継ぐことです。
インクルードは多重継承の代わりに用......いられており、 mix-in とも呼びます。
class C
include FileTest
include Math
end
p C.ancestors
# => [C, Math, FileTest, Object, Kernel]
モジュールの機能追加は、クラスの継承関係の間にそのモジュールが挿入
されることで実現され... -
Module
# included(class _ or _ module) -> () (6141) -
self が Module#include されたときに対象のクラスまたはモジュー ルを引数にしてインタプリタがこのメソッドを呼び出します。
... Module#include されたときに対象のクラスまたはモジュー
ルを引数にしてインタプリタがこのメソッドを呼び出します。
@param class_or_module Module#include を実行したオブジェクト
module Foo
def self.included(mod)
p "#{mod} include #{s......elf}"
end
end
class Bar
include Foo
end
# => "Bar include Foo"... -
Module
# include?(mod) -> bool (6120) -
self かその親クラス / 親モジュールがモジュール mod を インクルードしていれば true を返します。
...その親クラス / 親モジュールがモジュール mod を
インクルードしていれば true を返します。
@param mod Module を指定します。
module M
end
class C1
include M
end
class C2 < C1
end
p C1.include?(M) # => true
p C2.include?(M) # => true... -
Module
# included _ modules -> [Module] (6101) -
インクルードされているモジュールの配列を返します。
...インクルードされているモジュールの配列を返します。
@see Module#ancestors... -
Module
# const _ defined?(name , inherit = true) -> bool (31) -
モジュールに name で指定される名前の定数が定義されている時真 を返します。
...クラスや include したモジュールで定義された定数を検索対象
にするかどうかは第二引数で制御することができます。
@param name String, Symbol で指定される定数名。
@param inherit false を指定するとスーパークラスや include したモ......数は対象にはなりません。
module Kernel
FOO = 1
end
# Object は include したモジュールの定数に対しても
# true を返す
p Object.const_defined?(:FOO) # => true
module Bar
BAR = 1
end
class Object
include Bar
end
# ユーザ定義のモ......ジュールに対しても同様
p Object.const_defined?(:BAR) # => true
class Baz
include Bar
end
# Object 以外でも同様になった
# 第二引数のデフォルト値が true であるため
p Baz.const_defined?(:BAR) # => true
# 第二引数を false にした場... -
Module
# const _ defined?(name) -> bool (25) -
モジュールに name で指定される名前の定数が定義されている時真 を返します。
...。
スーパークラスや include したモジュールで定義された定数は対象には
なりません。(ただし、Object だけは例外)
@param name String, Symbol で指定される定数名。
module Kernel
FOO = 1
end
# Object は include したモジュールの定......返す
p Object.const_defined?(:FOO) # => true
module Bar
BAR = 1
end
class Object
include Bar
end
# ユーザ定義のモジュールに対しても同様
p Object.const_defined?(:BAR) # => true
class Baz
include Bar
end
# Object 以外では自身の定数だ... -
Module
# const _ get(name , inherit = true) -> object (25) -
モジュールに定義されている name で指定される名前の定数の値を 取り出します。
...数の値を
取り出します。
Module#const_defined? と違って Object を特別扱いすることはありません。
@param name 定数名。String か Symbol で指定します。
@param inherit false を指定するとスーパークラスや include したモジュールで
定......ていないときに発生します。
module Bar
BAR = 1
end
class Object
include Bar
end
# Object では include されたモジュールに定義された定数を見付ける
p Object.const_get(:BAR) # => 1
class Baz
include Bar
end
# Object以外でも同様... -
Module
# append _ features(module _ or _ class) -> self (19) -
モジュール(あるいはクラス)に self の機能を追加します。
...いはクラス)に self の機能を追加します。
このメソッドは Module#include の実体であり、
include を Ruby で書くと以下のように定義できます。
def include(*modules)
modules.each {|mod|
# append_features はプライベートメソッドなので......# 直接 mod.append_features(self) とは書けない
mod.__send__(:append_features, self)
mod.__send__(:included, self)
}
end... -
Module
# const _ get(name) -> object (19) -
モジュールに定義されている name で指定される名前の定数の値を 取り出します。
...
Module#const_defined? と違って Object を特別扱いすることはありません。
@param name 定数名。String か Symbol で指定します。
@raise NameError 定数が定義されていないときに発生します。
module Bar
BAR = 1
end
class Object
include Bar......end
# Object では include されたモジュールに定義された定数を見付ける
p Object.const_get(:BAR) # => 1
class Baz
include Bar
end
# Object以外でも同様
p Baz.const_get(:BAR) # => 1
# 定義されていない定数
p Baz.const_get(:NOT_DEFINED)... -
Module
# <(other) -> bool | nil (7) -
比較演算子。self が other の子孫である場合、 true を返します。 self が other の先祖か同一のクラス/モジュールである場合、false を返します。
...
module Foo
end
class Bar
include Foo
end
class Baz < Bar
end
class Qux
end
p Bar < Foo # => true
p Baz < Bar # => true
p Baz < Foo # => true
p Baz < Qux # => nil
p Baz > Qux # => nil
p Foo < Object.new # => in `<': compared with non class/module... -
Module
# <=>(other) -> Integer | nil (7) -
self と other の継承関係を比較します。
...er がクラスやモジュールでなければ
nil を返します。
@param other 比較対象のクラスやモジュール
module Foo
end
class Bar
include Foo
end
class Baz < Bar
end
class Qux
end
p Bar <=> Foo # => -1
p Baz <=> Bar # => -1
p Baz <=> Foo... -
Module
# ancestors -> [Class , Module] (7) -
クラス、モジュールのスーパークラスとインクルードしているモジュール を優先順位順に配列に格納して返します。
...ル
を優先順位順に配列に格納して返します。
module Foo
end
class Bar
include Foo
end
class Baz < Bar
p ancestors
p included_modules
p superclass
end
# => [Baz, Bar, Foo, Object, Kernel]
# => [Foo, Kernel]
# => Bar
@see Module#included_modules... -
Module
# constants(inherit = true) -> [Symbol] (7) -
そのモジュール(またはクラス)で定義されている定数名の配列を返します。
...スや include したモジュールで
定義された定数が対象にはなります。false を指定した場合 対象にはなりません。
@see Module.constants, Kernel.#local_variables, Kernel.#global_variables, Object#instance_variables, Module#class_variables
Module.constants......覧を取得して後で差し引く
$clist = Module.constants
class Foo
FOO = 1
end
class Bar
BAR = 1
# Bar は BAR を含む
p constants - $clist # => [:BAR]
# 出力に FOO は含まれない
p Module.constants - $clist # => [:BAR, :Bar, :F......# ネストしたクラスでは、外側のクラスで定義した定数は
# 参照可能なので、BAR は、Module.constants には含まれる
# (クラス Baz も Bar の定数なので同様)
p Module.constants - $clist # => [:BAR, :Baz, :Foo, :Bar]
end
end...
