254件ヒット
[101-200件を表示]
(0.103秒)
種類
- インスタンスメソッド (243)
- 特異メソッド (11)
ライブラリ
- ビルトイン (254)
キーワード
- < (11)
- <=> (11)
- ancestors (11)
-
class
_ eval (22) -
class
_ exec (11) -
class
_ variable _ get (11) -
class
_ variable _ set (11) -
class
_ variables (11) -
const
_ defined? (11) -
const
_ get (11) -
const
_ source _ location (5) - constants (22)
- include (11)
-
instance
_ method (11) -
instance
_ methods (11) -
module
_ eval (22) -
module
_ exec (11) -
private
_ constant (9) -
public
_ constant (9) -
remove
_ class _ variable (11) -
remove
_ const (11)
検索結果
先頭5件
-
Module
# module _ exec(*args) {|*vars| . . . } -> object (3122.0) -
与えられたブロックを指定された args を引数としてモジュールのコンテキストで評価します。
...
class Thing
end
c = 1
Thing.class_exec{
def hello()
"Hello there!"
end
define_method(:foo) do # ローカル変数がブロックの外側を参照している
c
end
}
t = Thing.new
p t.hello() #=> "Hello there!"
p t.foo() #=> 1
//}
@see Module#module......_eval, Module#class_eval... -
Module
# const _ get(name , inherit = true) -> object (150.0) -
name で指定される名前の定数の値を取り出します。
...name で指定される名前の定数の値を取り出します。
Module#const_defined? と違って Object を特別扱いすることはありません。
@param name 定数名。String か Symbol で指定します。
完全修飾名を指定しなかった場合はモジュー......m inherit false を指定するとスーパークラスや include したモジュールで
定義された定数は対象にはなりません。
@raise NameError 定数が定義されていないときに発生します。
//emlist[例][ruby]{
module Bar
BAR = 1
end
class Object
inclu......de 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) #=> raise Name... -
Module
# ancestors -> [Class , Module] (120.0) -
クラス、モジュールのスーパークラスとインクルードしているモジュール を優先順位順に配列に格納して返します。
...順位順に配列に格納して返します。
//emlist[例][ruby]{
module Foo
end
class Bar
include Foo
end
class Baz < Bar
p ancestors
p included_modules
p superclass
end
# => [Baz, Bar, Foo, Object, Kernel, BasicObject]
# => [Foo, Kernel]
# => Bar
//}
@see Module#included_modules......先順位順に配列に格納して返します。
//emlist[例][ruby]{
module Foo
end
class Bar
include Foo
end
class Baz < Bar
p ancestors
p included_modules
p superclass
end
# => [Baz, Bar, Foo, Object, Kernel, BasicObject]
# => [Foo, Kernel]
# => Bar
//}
@see Module#included_modules... -
Module
# remove _ const(name) -> object (120.0) -
name で指定した定数を取り除き、その定数に設定されていた値を 返します。
...rror 引数で指定された定数がそのモジュールやクラスに定義されていない場合に発生します。
//emlist[例][ruby]{
class Foo
FOO = 1
p remove_const(:FOO) # => 1
p FOO # => uninitialized constant FOO at Foo (NameError)
end
//}
組み込みクラス/モ......指定した(まだロードしてない)定数を含めて削除する事ができます。
取り除かれた定数は参照できなくなりますが、消える訳ではないので注意して
使用してください。
@see Module#remove_class_variable, Object#remove_instance_variable... -
Module
# const _ source _ location(name , inherited = true) -> [String , Integer] (61.0) -
name で指定した定数の定義を含むソースコードのファイル名と行番号を配列で返します。
...@param inherited true を指定するとスーパークラスや include したモジュールで定義された定数が対象にはなります。false を指定した場合 対象にはなりません。
@return ソースコードのファイル名と行番号を配列で返します。......った場合は空の配列を返します。
//emlist[例][ruby]{
# test.rb:
class A # line 1
C1 = 1
C2 = 2
end
module M # line 6
C3 = 3
end
class B < A # line 10
include M
C4 = 4
end
class A # 継続して A を定義する
C2 = 8 # 定数を再定義する
end......st_source_location('C3', false) # => nil -- include したモジュールは検索しない
p A.const_source_location('C2') # => ["test.rb", 16] -- 最後に定義された位置を返す
p Object.const_source_location('B') # => ["test.rb", 10] -- Object はトップレベル... -
Module
# constants(inherit = true) -> [Symbol] (55.0) -
そのモジュール(またはクラス)で定義されている定数名の配列を返します。
...クルードしているモジュールの定数も含みます。
Object のサブクラスの場合、Objectやそのスーパークラスで定義されている
定数は含まれません。 Object.constants とすると Object クラスで定義された
定数の配列が得られます。......た定数が対象にはなります。false を指定した場合 対象にはなりません。
@see Module.constants, Kernel.#local_variables, Kernel.#global_variables, Object#instance_variables, Module#class_variables
//emlist[Module.constants と Module#constants の違い][ruby]{
# 出力の......後で差し引く
$clist = Module.constants
class Foo
FOO = 1
end
class Bar
BAR = 1
# Bar は BAR を含む
p constants # => [:BAR]
# 出力に FOO は含まれない
p Module.constants - $clist # => [:BAR, :Bar, :Foo]
class Baz
# Baz は定数を... -
Module
# instance _ methods(inherited _ too = true) -> [Symbol] (55.0) -
そのモジュールで定義されている public および protected メソッド名 の一覧を配列で返します。
...メソッド名
の一覧を配列で返します。
@param inherited_too false を指定するとそのモジュールで定義されているメソッドのみ返します。
@see Object#methods
//emlist[例1][ruby]{
class Foo
private; def private_foo() end
protected; def protected_foo(......ethods(false)
p Foo.public_instance_methods(false)
p Foo.private_instance_methods(false)
p Foo.protected_instance_methods(false)
class Bar < Foo
end
//}
実行結果
[:protected_foo, :public_foo]
[:public_foo]
[:private_foo]
[:protected_foo]
//emlist[例2][ruby]{
class Bar......# いるが、Object のインスタンスメソッドは一覧から排除している。
p Bar.instance_methods(true) - Object.instance_methods(true)
p Bar.public_instance_methods(true) - Object.public_instance_methods(true)
p Bar.private_instance_methods(true) - Object.private_inst... -
Module
# const _ defined?(name , inherit = true) -> bool (43.0) -
モジュールに name で指定される名前の定数が定義されている時真 を返します。
...nherit false を指定するとスーパークラスや include したモジュールで
定義された定数は対象にはなりません。
//emlist[例][ruby]{
module Kernel
FOO = 1
end
# Object は include したモジュールの定数に対しても
# true を返す
p Object.cons......t_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 にした場合
p Baz.const_defined?(:BAR, false) # => false
//}... -
Module
# <(other) -> bool | nil (31.0) -
比較演算子。self が other の子孫である場合、 true を返します。 self が other の先祖か同一のクラス/モジュールである場合、false を返します。
...、false を返します。
継承関係にないクラス同士の比較では
nil を返します。
@param other 比較対象のモジュールやクラス
@raise TypeError other がクラスやモジュールではない場合に発生します。
//emlist[例][ruby]{
module Foo
end
class Ba......r
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 (TypeError)
//}... -
Module
# include(*mod) -> self (31.0) -
モジュール mod をインクルードします。
...ます。
@param mod Module のインスタンス( Enumerable など)を指定します。
@raise ArgumentError 継承関係が循環してしまうような include を行った場合に発生します。
//emlist[例][ruby]{
module M
end
module M2
include M
end
module M
include M2
end
//}......多重継承の代わりに用いられており、 mix-in とも呼びます。
//emlist[例][ruby]{
class C
include FileTest
include Math
end
p C.ancestors
# => [C, Math, FileTest, Object, Kernel]
//}
モジュールの機能追加は、クラスの継承関係の間にそのモジュー......先に行われます
(上の例の Module#ancestors の結果がメソッド探索の順序です)。
同じモジュールを二回以上 include すると二回目以降は無視されます。
//emlist[例][ruby]{
module M
end
class C1
include M
end
class C2 < C1
include M # この incl...