るりまサーチ

最速Rubyリファレンスマニュアル検索!
749件ヒット [301-400件を表示] (0.080秒)
トップページ > クエリ:Ruby[x] > クエリ:ruby[x] > 種類:インスタンスメソッド[x] > クエリ:-[x] > クエリ:@[x] > クラス:Object[x]

別のキーワード

  1. fiddle ruby_free
  2. rbconfig ruby
  3. fiddle build_ruby_platform
  4. rake ruby
  5. rubygems/defaults ruby_engine

ライブラリ

キーワード

検索結果

<< < ... 2 3 4 5 6 ... > >>

Object#enum_for(method = :each, *args) {|*args| ... } -> Enumerator (144.0)

Enumerator.new(self, method, *args) を返します。

...果を返
します。ブロックパラメータは引数 args です。


@
param method メソッド名の文字列かシンボルです。
@
param args 呼び出すメソッドに渡される引数です。

//emlist[][ruby]{
str = "xyz"

enum = str.enum_for(:each_byte)
p(a = enum.map{|b| '%02x' %...
...cts an array from being modified
a = [1, 2, 3]
p(a.to_enum) #=> #<Enumerator: [1, 2, 3]:each>
//}

//emlist[例(ブロックを指定する場合)][ruby]{
module Enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
# __method__ はここで...
...each do |*val|
n.times { yield *val }
end
end
end

%i[hello world].repeat(2) { |w| puts w }
# => 'hello', 'hello', 'world', 'world'
enum = (1..14).repeat(3)
# => #<Enumerator: 1..14:repeat(3)>
enum.first(4) # => [1, 1, 1, 2]
enum.size # => 42
//}

@
see Enumerator, Enumerator#size...

Object#extend(*modules) -> self (144.0)

引数で指定したモジュールのインスタンスメソッドを self の特異 メソッドとして追加します。

...順に extend を行います。

@
param modules モジュールを任意個指定します(クラスは不可)。
@
return self を返します。

//emlist[][ruby]{
module Foo
def a
'ok Foo'
end
end

module Bar
def b
'ok Bar'
end
end

obj = Object.new
obj.extend Foo, Bar
p obj.a...
...ただしその場合、フック用のメソッド
が Module#extended ではなく Module#included になるという違いがあります。

//emlist[][ruby]{
# obj.extend Foo, Bar とほぼ同じ
class << obj
include Foo, Bar
end
//}

@
see Module#extend_object,Module#include,Module#extended...

Object#remove_instance_variable(name) -> object (144.0)

オブジェクトからインスタンス変数 name を取り除き、そのインス タンス変数に設定されていた値を返します。

...を返します。

@
param name 削除するインスタンス変数の名前をシンボルか文字列で指定します。
@
raise NameError オブジェクトがインスタンス変数 name を持たない場合に発生します。

//emlist[][ruby]{
class Foo
def foo
@
foo = 1
p remov...
...e_instance_variable(:@foo) #=> 1
p remove_instance_variable(:@foo) # instance variable @foo not defined (NameError)
end
end
Foo.new.foo
//}

@
see Module#remove_class_variable,Module#remove_const...

Object#singleton_methods(inherited_too = true) -> [Symbol] (144.0)

そのオブジェクトに対して定義されている特異メソッド名 (public あるいは protected メソッド) の一覧を返します。

...ドとは Object#extend によって追加された特異メソッドや、
self がクラスの場合はスーパークラスのクラスメソッド(Classのインスタンスの特異メソッド)などです。

singleton_methods(false) は、Object#methods(false) と同じです。

@
param inhe...
...継承した特異メソッドを含める場合は真を、
そうでない場合は偽を指定します。

//emlist[例1][ruby]{
Parent = Class.new

class <<Parent
private; def private_class_parent() end
protected; def protected_class_parent() end
public; def pub...
...2][ruby]{
# あるオブジェクトの特異メソッドの一覧を得る。
# 親クラスのクラスメソッドも含まれるよう true を指定したが、
# Object のクラスメソッドは一覧から排除している。

p obj.singleton_methods(true)
p Foo.singleton_methods(true) - O...

Object#tainted? -> bool (144.0)

...オブジェクトの汚染に関してはspec/safelevelを参照してください。

//emlist[][ruby]{
p String.new.tainted? #=> false
p ENV['OS'].tainted? #=> true
//}

このメソッドは Ruby 2.7から deprecated で、Ruby 3.2 で削除予定です。

@
see Object#taint,Object#untaint...

絞り込み条件を変える

Object#to_enum(method = :each, *args) -> Enumerator (144.0)

Enumerator.new(self, method, *args) を返します。

...果を返
します。ブロックパラメータは引数 args です。


@
param method メソッド名の文字列かシンボルです。
@
param args 呼び出すメソッドに渡される引数です。

//emlist[][ruby]{
str = "xyz"

enum = str.enum_for(:each_byte)
p(a = enum.map{|b| '%02x' %...
...cts an array from being modified
a = [1, 2, 3]
p(a.to_enum) #=> #<Enumerator: [1, 2, 3]:each>
//}

//emlist[例(ブロックを指定する場合)][ruby]{
module Enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
# __method__ はここで...
...each do |*val|
n.times { yield *val }
end
end
end

%i[hello world].repeat(2) { |w| puts w }
# => 'hello', 'hello', 'world', 'world'
enum = (1..14).repeat(3)
# => #<Enumerator: 1..14:repeat(3)>
enum.first(4) # => [1, 1, 1, 2]
enum.size # => 42
//}

@
see Enumerator, Enumerator#size...

Object#to_enum(method = :each, *args) {|*args| ... } -> Enumerator (144.0)

Enumerator.new(self, method, *args) を返します。

...果を返
します。ブロックパラメータは引数 args です。


@
param method メソッド名の文字列かシンボルです。
@
param args 呼び出すメソッドに渡される引数です。

//emlist[][ruby]{
str = "xyz"

enum = str.enum_for(:each_byte)
p(a = enum.map{|b| '%02x' %...
...cts an array from being modified
a = [1, 2, 3]
p(a.to_enum) #=> #<Enumerator: [1, 2, 3]:each>
//}

//emlist[例(ブロックを指定する場合)][ruby]{
module Enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
# __method__ はここで...
...each do |*val|
n.times { yield *val }
end
end
end

%i[hello world].repeat(2) { |w| puts w }
# => 'hello', 'hello', 'world', 'world'
enum = (1..14).repeat(3)
# => #<Enumerator: 1..14:repeat(3)>
enum.first(4) # => [1, 1, 1, 2]
enum.size # => 42
//}

@
see Enumerator, Enumerator#size...

Object#==(other) -> bool (138.0)

オブジェクトと other が等しければ真を返します。

...では equal? と同じオブジェクト
の同一性判定になっています。

@
param other 比較するオブジェクトです。

//emlist[例][ruby]{
o = Object.new
p(o.eql?(o)) #=> true
p(o.eql?(Object.new)) #=> false
//}

下記の例のように、各クラスの性質に合わせて...
...再定義されることが期待されています。

//emlist[適切に再定義されている例][ruby]{
p("foo" == "bar") #=> false
p("foo" == "foo") #=> true

p(4 == 4) #=> true
p(4 == 4.0) #=> true
//}

@
see Object#equal?,Object#eql?...

Object#=~(other) -> nil (138.0)

右辺に正規表現オブジェクトを置いた正規表現マッチ obj =~ /RE/ をサポートするためのメソッドです。常に nil を返します。

...nil を返します。

このメソッドは Ruby 2.6 から deprecated です。

この定義により、=~ が再定義されたオブジェクトでは正常にマッチを行い、
それ以外のものは nil を返すようになります。


@
param other 任意のオブジェクトです...
...。結果に影響しません。

//emlist[例][ruby]{
obj = 'regexp'
p(obj =~ /re/) #=> 0

obj = nil
p(obj =~ /re/) #=> nil
//}

@
see String#=~...
... Ruby 2.6 から deprecated です。

意図せずに Array などに対して呼ばれた時にバグの原因になっていたため、
代わりに NilClass#=~ が定義されています。

@
param other 任意のオブジェクトです。結果に影響しません。

//emlist[例][ruby]{
o...
...bj = 'regexp'
p(obj =~ /re/) #=> 0

obj = nil
p(obj =~ /re/) #=> nil
//}

@
see String#=~...

Object#freeze -> self (138.0)

オブジェクトを凍結(内容の変更を禁止)します。

...l.#trace_var が使えます。

@
return self を返します。

//emlist[][ruby]{
a1 = "foo".freeze
a1 = "bar"
p a1 #=> "bar"

a2 = "foo".freeze
a2.replace("bar") # can't modify frozen String (FrozenError)
//}

凍結を解除することはできませんが、Object#dup を使えばほぼ同じ...
...ます。

//emlist[][ruby]{
a = [1].freeze
p a.frozen? #=> true

a[0] = "foo"
p a # can't modify frozen Array (FrozenError)

b = a.dup
p b #=> [1]
p b.frozen? #=> false

b[0] = "foo"
p b #=> ["foo"]
//}

@
see Object#frozen?,Object#dup,Kernel.#trace_var...

絞り込み条件を変える

<< < ... 2 3 4 5 6 ... > >>