108件ヒット
[1-100件を表示]
(0.037秒)
種類
- インスタンスメソッド (96)
- 特異メソッド (12)
ライブラリ
- ビルトイン (108)
キーワード
- autoload (12)
-
const
_ missing (12) -
const
_ set (12) - constants (24)
-
deprecate
_ constant (12) -
private
_ constant (12) -
public
_ constant (12) -
remove
_ const (12)
検索結果
先頭5件
-
Module
# private _ constant(*name) -> self (12232.0) -
name で指定した定数の可視性を private に変更します。
...。
@see Module#public_constant, Object#untrusted?
//emlist[例][ruby]{
module Foo
BAR = 'bar'
class Baz; end
QUX = 'qux'
class Quux; end
private_constant :QUX
private_constant :Quux
end
Foo::BAR # => "bar"
Foo::Baz # => Foo::Baz
Foo::QUX # => NameError: private constant Foo::QUX......referenced
Foo::Quux # => NameError: private constant Foo::Quux referenced
//}......します。
@see Module#public_constant
//emlist[例][ruby]{
module Foo
BAR = 'bar'
class Baz; end
QUX = 'qux'
class Quux; end
private_constant :QUX
private_constant :Quux
end
Foo::BAR # => "bar"
Foo::Baz # => Foo::Baz
Foo::QUX # => NameError: private constant Foo::QUX referenced......Foo::Quux # => NameError: private constant Foo::Quux referenced
//}... -
Module
# deprecate _ constant(*name) -> self (12226.0) -
name で指定した定数を deprecate に設定します。 deprecate に設定した定数を参照すると警告メッセージが表示されます。
...になったため、
デフォルトでは警告が表示されません。
コマンドラインオプション(詳細はd:spec/rubycmd#cmd_option参照)で、
「-w」か「-W2」などを指定するか、実行中に「Warning[:deprecated] = true」で
変更すると表示されるように......い定数を指定した場合に発生します。
@return self を返します。
//emlist[例][ruby]{
FOO = 123
Object.deprecate_constant(:FOO) # => Object
FOO
# warning: constant ::FOO is deprecated
# => 123
Object.deprecate_constant(:BAR)
# NameError: constant Object::BAR not defined
//}... -
Module
# public _ constant(*name) -> self (12226.0) -
name で指定した定数の可視性を public に変更します。
...][ruby]{
module SampleModule
class SampleInnerClass
end
# => 非公開クラスであることを明示するために private にする
private_constant :SampleInnerClass
end
begin
SampleModule::SampleInnerClass
rescue => e
e # => #<NameError: private constant SampleModule::SampleInne......rClass referenced>
end
module SampleModule
# => 非公開クラスであることは承知で利用するために public にする
public_constant :SampleInnerClass
end
SampleModule::SampleInnerClass # => SampleModule::SampleInnerClass
//}
@see Module#private_constant, Object#untrusted?......rClass referenced>
end
module SampleModule
# => 非公開クラスであることは承知で利用するために public にする
public_constant :SampleInnerClass
end
SampleModule::SampleInnerClass # => SampleModule::SampleInnerClass
//}
@see Module#private_constant... -
Module
# constants(inherit = true) -> [Symbol] (12201.0) -
そのモジュール(またはクラス)で定義されている定数名の配列を返します。
...ます。
Object のサブクラスの場合、Objectやそのスーパークラスで定義されている
定数は含まれません。 Object.constants とすると Object クラスで定義された
定数の配列が得られます。
得られる定数の順序は保証されません。
@p......ee Module.constants, Kernel.#local_variables, Kernel.#global_variables, Object#instance_variables, Module#class_variables
//emlist[Module.constants と Module#constants の違い][ruby]{
# 出力の簡略化のため起動時の定数一覧を取得して後で差し引く
$clist = Module.constants......ss Bar
BAR = 1
# Bar は BAR を含む
p constants # => [:BAR]
# 出力に FOO は含まれない
p Module.constants - $clist # => [:BAR, :Bar, :Foo]
class Baz
# Baz は定数を含まない
p constants # => []
# ネス... -
Module
. constants -> [Symbol] (12201.0) -
このメソッドを呼び出した時点で参照可能な定数名の配列を返します。
...//emlist[例][ruby]{
class C
FOO = 1
end
p Module.constants # => [:RUBY_PLATFORM, :STDIN, ..., :C, ...]
# 出力中に :FOO は現われない
//}
@see Module#constants, Kernel.#local_variables, Kernel.#global_variables, Object#instance_variables, Module#class_variables... -
Module
# const _ missing(name) (6119.0) -
定義されていない定数を参照したときに Ruby インタプリタが このメソッドを呼びます。
...mbol
@raise NameError このメソッドを呼び出した場合、デフォルトで発生する例外
//emlist[例][ruby]{
class Foo
def Foo.const_missing(id)
warn "undefined constant #{id.inspect}"
end
Bar
end
Foo::Bar
# => undefined constant :Bar
# undefined constant :Bar
//}... -
Module
# const _ set(name , value) -> object (6113.0) -
モジュールに name で指定された名前の定数を value とい う値として定義し、value を返します。
...{
module Foo; end
# Symbolを指定した場合
Foo.const_set(:FOO, 123)
Foo::FOO # => 123
# Stringを指定した場合
Foo.const_set('BAR', 'abc')
Foo::BAR # => "abc"
# 既に定義されている定数の名前を指定した場合
Foo.const_set('BAR', '123')
# warning: already initialized con......stant Foo::BAR
# warning: previous definition of BAR was here
# => "123"
# 不適切な定数名を指定した場合
Foo.const_set('foo', 1) # => NameError: wrong constant name foo
//}... -
Module
# remove _ const(name) -> object (6107.0) -
name で指定した定数を取り除き、その定数に設定されていた値を 返します。
...クラスに定義されていない場合に発生します。
//emlist[例][ruby]{
class Foo
FOO = 1
p remove_const(:FOO) # => 1
p FOO # => uninitialized constant FOO at Foo (NameError)
end
//}
組み込みクラス/モジュールを設定している定数や Kernel.#autoload を......指定した(まだロードしてない)定数を含めて削除する事ができます。
取り除かれた定数は参照できなくなりますが、消える訳ではないので注意して
使用してください。
@see Module#remove_class_variable, Object#remove_instance_variable... -
Module
# autoload(const _ name , feature) -> nil (107.0) -
定数 const_name を最初に参照した時に feature を Kernel.#require するように設定します。
...定数 const_name を最初に参照した時に feature を Kernel.#require するように設定します。
const_name が autoload 設定されていて、まだ定義されてない(ロードされていない)ときは、
autoload する対象を置き換えます。
const_name が(autoload......ではなく)既に定義されているときは何もしません。
@param const_name String または Symbol で指定します。
なお、const_name には、"::" 演算子を含めることはできません。
つまり、self の直下に定義された定数しか指定で......ない場
合、NameError が発生します。
//emlist[例][ruby]{
# ------- /tmp/bar.rb ---------
class Bar
end
# ----- end of /tmp/bar.rb ----
class Foo
autoload :Bar, '/tmp/bar.rb'
end
p Foo::Bar
#=> -:4:in `<main>': uninitialized constant Foo::Bar (NameError)
//}
@see Kernel.#autoload...