ライブラリ
- ビルトイン (254)
キーワード
- < (11)
- <=> (11)
- === (11)
-
alias
_ method (11) -
class
_ eval (22) -
class
_ exec (11) -
class
_ variable _ set (11) -
define
_ method (22) - extended (11)
- inspect (11)
-
instance
_ method (11) -
module
_ eval (22) -
module
_ exec (11) - name (11)
- prepend (11)
- private (23)
- refine (11)
-
to
_ s (11) -
undef
_ method (11)
検索結果
先頭5件
-
Module
# prepend(*modules) -> self (8014.0) -
指定したモジュールを self の継承チェインの先頭に「追加する」ことで self の定数、メソッド、モジュール変数を「上書き」します。
...追加されるため、結果として self で定義されたメソッドは
override されます。
modules で指定したモジュールは後ろから順に処理されるため、
modules の先頭が最も優先されます。
また、継承によってこの「上書き」を処理す......のモジュール/クラスのメソッドを呼び出すことができます。
実際の処理は modules の各要素の prepend_features を後ろから順に呼びだすだけです。
Module#prepend_features が継承チェインの改変を実行し、結果として上のような
処理......とで
prepend の処理を追加/変更できます。
@param modules prepend する Module を指定します
@see Module#prepend_features, Module#prepended
//emlist[例][ruby]{
# super と prepend の組み合わせの例
module X
def foo
puts "X1" # (1x)
super # (2x)
puts "X2" #... -
Module
# refine(klass) { . . . } -> Module (8014.0) -
引数 klass で指定したクラスだけに対して、ブロックで指定した機能を提供で きるモジュールを定義します。定義した機能は Module#refine を使用せずに直 接 klass に対して変更を行う場合と異なり、限られた範囲のみ有効にできます。 そのため、既存の機能を局所的に修正したい場合などに用いる事ができます。
...したクラスだけに対して、ブロックで指定した機能を提供で
きるモジュールを定義します。定義した機能は Module#refine を使用せずに直
接 klass に対して変更を行う場合と異なり、限られた範囲のみ有効にできます。
そのた......ticles/0041/0041-200Special-refinement.html
* https://docs.ruby-lang.org/en/master/syntax/refinements_rdoc.html
定義した機能は main.using, Module#using を実行した場合のみ
有効になります。
@param klass 拡張する対象のクラスを指定します。
@return ブロッ......機能を持つ無名のモジュールを返します。
//emlist[例][ruby]{
class C
def foo
puts "C#foo"
end
end
module M
refine C do
def foo
puts "C#foo in M"
end
end
end
x = C.new
x.foo # => "C#foo"
using M
x = C.new
x.foo # => "C#foo in M"
//}
@see main.using......モジュールだけに対して、ブロックで指定した機能を提供で
きるモジュールを定義します。定義した機能は Module#refine を使用せずに直
接 klass に対して変更を行う場合と異なり、限られた範囲のみ有効にできます。
そのた......ticles/0041/0041-200Special-refinement.html
* https://docs.ruby-lang.org/en/master/syntax/refinements_rdoc.html
定義した機能は main.using, Module#using を実行した場合のみ
有効になります。
@param klass 拡張する対象のクラスまたはモジュールを指定します......機能を持つ無名のモジュールを返します。
//emlist[例][ruby]{
class C
def foo
puts "C#foo"
end
end
module M
refine C do
def foo
puts "C#foo in M"
end
end
end
x = C.new
x.foo # => "C#foo"
using M
x = C.new
x.foo # => "C#foo in M"
//}
@see main.using... -
Module
# <(other) -> bool | nil (8008.0) -
比較演算子。self が other の子孫である場合、 true を返します。 self が other の先祖か同一のクラス/モジュールである場合、false を返します。
...list[例][ruby]{
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 (TypeError)
//... -
Module
# <=>(other) -> Integer | nil (8008.0) -
self と other の継承関係を比較します。
...のクラスやモジュール
//emlist[例][ruby]{
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 # => -1
p Baz <=> Qux # => nil
p Qux <=> Baz # => nil
p Baz <=> Object.new # => nil
//}... -
Module
# ===(obj) -> bool (8008.0) -
指定された obj が self かそのサブクラスのインスタンスであるとき真を返します。 また、obj が self をインクルードしたクラスかそのサブクラスのインスタンスである場合にも 真を返します。上記のいずれでもない場合に false を返します。
...れます。
case ではクラス、モジュールの所属関係をチェックすることになります。
//emlist[例][ruby]{
str = String.new
case str
when String # String === str を評価する
p true # => true
end
//}
@param obj 任意のオブジェクト
@see Object#kind... -
Module
# class _ exec(*args) {|*vars| . . . } -> object (8008.0) -
与えられたブロックを指定された args を引数としてモジュールのコンテキストで評価します。
...ing.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
# class _ variable _ set(name , val) -> object (8008.0) -
クラス/モジュールにクラス変数 name を定義して、その値として val をセットします。val を返します。
...をセットします。val を返します。
@param name String または Symbol を指定します。
//emlist[例][ruby]{
class Fred
@@foo = 99
def foo
@@foo
end
end
def Fred.foo(val)
class_variable_set(:@@foo, val)
end
p Fred.foo(101) # => 101
p Fred.new.foo # => 101
//}... -
Module
# define _ method(name) { . . . } -> Symbol (8008.0) -
インスタンスメソッド name を定義します。
...。
@raise TypeError method に同じクラス、サブクラス、モジュール以外のメソッ
ドを指定した場合に発生します。
//emlist[例][ruby]{
class Foo
def foo() p :foo end
define_method(:bar, instance_method(:foo))
end
Foo.new.bar # => :foo
//}... -
Module
# define _ method(name , method) -> Symbol (8008.0) -
インスタンスメソッド name を定義します。
...。
@raise TypeError method に同じクラス、サブクラス、モジュール以外のメソッ
ドを指定した場合に発生します。
//emlist[例][ruby]{
class Foo
def foo() p :foo end
define_method(:bar, instance_method(:foo))
end
Foo.new.bar # => :foo
//}... -
Module
# extended(obj) -> () (8008.0) -
self が他のオブジェクト に Object#extend されたときに 呼ばれます。引数には extend を行ったオブジェクトが渡されます。
...ったオブジェクトが渡されます。
@param obj Object#extend を行ったオブジェクト
//emlist[例][ruby]{
module Foo
def self.extended(obj)
p "#{obj} extend #{self}"
end
end
Object.new.extend Foo
# => "#<Object:0x401cbc3c> extend Foo"
//}
@see Module#extend_object...