るりまサーチ

最速Rubyリファレンスマニュアル検索!
132件ヒット [1-100件を表示] (0.034秒)
トップページ > クエリ:Ruby[x] > クエリ:sample[x] > ライブラリ:ビルトイン[x]

別のキーワード

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

クラス

モジュール

キーワード

検索結果

<< 1 2 > >>

Array#sample -> object | nil (18142.0)

配列の要素を1個(引数を指定した場合は自身の要素数を越えない範囲で n 個) ランダムに選んで返します。

...

//emlist[例][ruby]{
a = (1..10).to_a
p a.sample #=> 9
p a.sample #=> 10
p a.sample(3) #=> [1, 9, 3]
p a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
//}

random SecureRandom などの乱数生成器を渡すことができます。

//emlist[例][ruby]{
require 'securer...
...andom'
a = (1..10).to_a
p a.sample(random: SecureRandom) #=> 2
//}...

Array#sample(n) -> Array (18142.0)

配列の要素を1個(引数を指定した場合は自身の要素数を越えない範囲で n 個) ランダムに選んで返します。

...

//emlist[例][ruby]{
a = (1..10).to_a
p a.sample #=> 9
p a.sample #=> 10
p a.sample(3) #=> [1, 9, 3]
p a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
//}

random SecureRandom などの乱数生成器を渡すことができます。

//emlist[例][ruby]{
require 'securer...
...andom'
a = (1..10).to_a
p a.sample(random: SecureRandom) #=> 2
//}...

Array#sample(n, random: Random) -> Array (18142.0)

配列の要素を1個(引数を指定した場合は自身の要素数を越えない範囲で n 個) ランダムに選んで返します。

...

//emlist[例][ruby]{
a = (1..10).to_a
p a.sample #=> 9
p a.sample #=> 10
p a.sample(3) #=> [1, 9, 3]
p a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
//}

random SecureRandom などの乱数生成器を渡すことができます。

//emlist[例][ruby]{
require 'securer...
...andom'
a = (1..10).to_a
p a.sample(random: SecureRandom) #=> 2
//}...

Array#sample(random: Random) -> object | nil (18142.0)

配列の要素を1個(引数を指定した場合は自身の要素数を越えない範囲で n 個) ランダムに選んで返します。

...

//emlist[例][ruby]{
a = (1..10).to_a
p a.sample #=> 9
p a.sample #=> 10
p a.sample(3) #=> [1, 9, 3]
p a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
//}

random SecureRandom などの乱数生成器を渡すことができます。

//emlist[例][ruby]{
require 'securer...
...andom'
a = (1..10).to_a
p a.sample(random: SecureRandom) #=> 2
//}...

Object#respond_to_missing?(symbol, include_private) -> bool (68.0)

自身が symbol で表されるメソッドに対し BasicObject#method_missing で反応するつもりならば真を返します。

...e private method も含めたい場合に true が渡されます

//emlist[例][ruby]{
class Sample
def method_missing(name, *args)
if name =~ /^to_*/
[name, *args] # => [:to_sample, "sample args1", "sample args2"]
return
else
super
end
end

def respond_to_missin...
...g?(sym, include_private)
(sym =~ /^to_*/) ? true : super
end
end

s = Sample.new
s.to_sample("sample args1", "sample args2")
s.respond_to?(:to_sample) # => true
s.respond_to?(:sample) # => false
//}

@see Object#respond_to?, BasicObject#method_missing...

絞り込み条件を変える

ObjectSpace.#undefine_finalizer(obj) -> object (44.0)

obj に対するファイナライザをすべて解除します。 obj を返します。

...st[例][ruby]{
class Sample
def Sample.callback
proc {
puts "finalize"
}
end

def initialize
ObjectSpace.define_finalizer(self, Sample.callback)
end

def undef
ObjectSpace.undefine_finalizer(self)
end
end

Sample
.new
GC.start
# => finalize

Sample
.new
sample
.undef
G...

Kernel.#abort -> () (30.0)

Ruby プログラムをエラーメッセージ付きで終了します。終了ステータスは 1 固定です。

...
Ruby
プログラムをエラーメッセージ付きで終了します。終了ステータスは 1 固定です。

このメソッドと Kernel.#exit との違いは、プログラムの終了ステー
タスが 1 (正確にはCレベルの定数 EXIT_FAILURE の値)固定であることと、...
...その例外のメッセージと
バックトレースを表示します。

@param message エラーメッセージ文字列です。

//emlist[][ruby]{
puts 'start'
begin
puts 'start1...'
abort "error1"
rescue SystemExit => err
puts "end1 with #{err.inspect}"
end

begin
puts 'start2...'...
...#(標準出力)
#=> start
# start1...
# end1 with #<SystemExit: error1>
# start2...
# end2...
#終了ステータス:1
#(標準エラー出力)
#=> error1
# Traceback (most recent call last):
# sample.rb:11:in `<main>': RuntimeError (RuntimeError)
//}

@see Kernel.#exit,Kernel.#exit!...

Kernel.#abort(message) -> () (30.0)

Ruby プログラムをエラーメッセージ付きで終了します。終了ステータスは 1 固定です。

...
Ruby
プログラムをエラーメッセージ付きで終了します。終了ステータスは 1 固定です。

このメソッドと Kernel.#exit との違いは、プログラムの終了ステー
タスが 1 (正確にはCレベルの定数 EXIT_FAILURE の値)固定であることと、...
...その例外のメッセージと
バックトレースを表示します。

@param message エラーメッセージ文字列です。

//emlist[][ruby]{
puts 'start'
begin
puts 'start1...'
abort "error1"
rescue SystemExit => err
puts "end1 with #{err.inspect}"
end

begin
puts 'start2...'...
...#(標準出力)
#=> start
# start1...
# end1 with #<SystemExit: error1>
# start2...
# end2...
#終了ステータス:1
#(標準エラー出力)
#=> error1
# Traceback (most recent call last):
# sample.rb:11:in `<main>': RuntimeError (RuntimeError)
//}

@see Kernel.#exit,Kernel.#exit!...

Proc#binding -> Binding (20.0)

Proc オブジェクトが保持するコンテキストを Binding オブジェクトで返します。

...Proc オブジェクトが保持するコンテキストを
Binding オブジェクトで返します。

//emlist[例][ruby]{
def fred(param)
proc {}
end

sample
_proc = fred(99)
eval("param", sample_proc.binding) # => 99
//}...

Random#rand -> Float (20.0)

一様な擬似乱数を発生させます。

...、引数に負の数値を与えた時や (1..0) のような値が存在しない範囲を渡した時などに発生します。

//emlist[例][ruby]{
# Kernel.#rand とほぼ同様の使い勝手
prng = Random.new(1234)
prng.rand # => 0.1915194503788923
srand(1234)
rand...
...うに見える

# 上と同じ種で再初期化
prng = Random.new(1234)
srand(1234)
# Kernel.#rand は Array#sample などの影響を受けて値がずれることがある
[0, 1].sample
prng.rand #=> 0.1915194503788923
rand #=> 0.6221087710398319
//}


@see Kernel.#r...

絞り込み条件を変える

<< 1 2 > >>