るりまサーチ (Ruby 2.5.0)

最速Rubyリファレンスマニュアル検索!
3件ヒット [1-3件を表示] (0.022秒)

別のキーワード

  1. argf.class lines
  2. argf.class each
  3. argf.class each_line
  4. class new
  5. argf.class to_a

キーワード

検索結果

Method (154.0)

Object#method によりオブジェクト化され たメソッドオブジェクトのクラスです。

...、Method は大規模コードに向くと言えます。


既存のメソッドを Method オブジェクト化する。

//emlist[例][ruby]{
class
Foo
def foo(arg)
"foo called with arg #{arg}"
end
end

m = Foo.new.method(:foo)

p m # => #<Method: Foo#foo>
p m.call(1) # =...
...cc>
p pr.call(1) # => "proc called with arg 1"
//}

Method オブジェクトが有用なのは以下のような場合。

//emlist[例][ruby]{
class
Foo
def foo() "foo" end
def bar() "bar" end
def baz() "baz" end
end

obj = Foo.new

# 任意のキーとメソッドの関係をハッシュ...
...(Method オブジェクトはレシーバを保持する)必
要がないなら Object#public_sendを使う方法も有用。

//emlist[例][ruby]{
class
Foo
def foo() "foo" end
def bar() "bar" end
def baz() "baz" end
end

# 任意のキーとメソッド(の名前)の関係をハッシュに...

UnboundMethod (118.0)

レシーバを持たないメソッドを表すクラスです。 呼び出すためにはレシーバにバインドする必要があります。

...とができます。

//emlist[例: Method クラスの冒頭にある例を UnboundMethod で書くと以下のようになります。][ruby]{
class
Foo
def foo() "foo" end
def bar() "bar" end
def baz() "baz" end
end

# 任意のキーとメソッドの関係をハッシュに保持して...
...mlist[例: 以下はメソッドの再定義を UnboundMethod を使って行う方法です。普通は alias や super を使います。][ruby]{
class
Foo
def foo
p :foo
end
@@orig_foo = instance_method :foo
def foo
p :bar
@@orig_foo.bind(self).call
end
end

Foo.new.foo

# =...

Thread::Backtrace::Location (100.0)

Ruby のフレームを表すクラスです。

...例1の実行結果:

caller_locations.rb:2:in `a'
caller_locations.rb:5:in `b'
caller_locations.rb:8:in `c'

//emlist[例2][ruby]{
# foo.rb
class
Foo
attr_accessor :locations
def initialize(skip)
@locations = caller_locations(skip)
end
end

Foo.new(0..2).locations.map do |call|
p...