るりまサーチ

最速Rubyリファレンスマニュアル検索!
73件ヒット [1-73件を表示] (0.083秒)
トップページ > クエリ:l[x] > クエリ:fetch[x] > クエリ:new[x]

別のキーワード

  1. kernel $-l
  2. _builtin $-l
  3. matrix l
  4. lupdecomposition l
  5. $-l kernel

ライブラリ

クラス

モジュール

キーワード

検索結果

Net::HTTPHeader#fetch(key, default) -> String (18245.0)

key ヘッダフィールドを返します。

...を返します。

たとえばキー 'content-length' に対しては '2048'
のような文字列が得られます。キーが存在しなければ nil を返します。

該当するキーが登録されてい
ない時には、引数 default が与えられていればその値を、ブロ...
...数defaultもブロックも与えられてない時、キーの探索に 失敗すると発生します。

//emlist[例 key のみ指定。key が存在する][ruby]{
require 'net/http'

uri = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Get.new(uri.request_uri)
req.fetch("user-...
...//emlist[例 key のみ指定。key が存在しない][ruby]{
require 'net/http'

begin
req.fetch("content-length")
rescue => e
e # => #<KeyError: key not found: "content-length">
end
//}

//emlist[例 key , default を指定][ruby]{
require 'net/http'

uri = URI.parse('http://www.example.com...

Thread#fetch(name, default = nil) {|name| ... } -> object (18243.0)

name に関連づけられたスレッドに固有のデータを返します。 name に対応するスレッド固有データがない時には、引数 default が 与えられていればその値を、ブロックが与えられていれば そのブロックを評価した値を返します。

...時には、引数 default が
与えられていればその値を、ブロックが与えられていれば
そのブロックを評価した値を返します。

@param name スレッド固有データのキーを文字列か Symbol で指定します。
@param default name に対応するスレ...
...defaultもブロックも与えられてない時、
name に対応するスレッド固有データがないと発生します。

//emlist[例][ruby]{
th = Thread.new { Thread.current[:name] = 'A' }
th.join
th.fetch(:name) # => "A"
th.fetch(:fetch, 'B') # => "B"
th.fetch('name'...
...) {|name| "Thread" + name} # => "A"
th.fetch('fetch') {|name| "Thread" + name} # => "Threadfetch"
//}

@see Thread#[]...

PStore#fetch(name, default = PStore::Error) -> object (18231.0)

ルートnameに対応する値を得ます。

...ルートが登録されていない時には、
引数 default が与えられていればその値を返し、
与えられていなければ例外 PStore::Error が発生します。

@param name 探索するルート。

@param default name に対応するルートが登録されていない場...
...かつ、
default が与えられていない場合に発生します。
また、トランザクション外でこのメソッドが呼び出された場合に発生します。

例:

require 'pstore'
db = PStore.new("/tmp/foo")
db.transaction do...
...]
ary = db["root"] = [1,2,3,4]
ary[0] = [1,1.5]
end

db.transaction(true) do |pstore|
pstore.fetch("root") # => [[1, 1.5], 2, 3, 4]
pstore.fetch("root", 'aaa') # => [[1, 1.5], 2, 3, 4]
pstore.fetch("not_root") # => 例外発生
end

@see Hash#fetch, PStore#[]...

Hash.new(ifnone = nil) -> Hash (18230.0)

空の新しいハッシュを生成します。ifnone はキーに対 応する値が存在しない時のデフォルト値です。設定したデフォルト値はHash#defaultで参照できます。

...faultで参照できます。

ifnoneを省略した Hash.new は {} と同じです。

デフォルト値として、毎回同一のオブジェクトifnoneを返します。
それにより、一箇所のデフォルト値の変更が他の値のデフォルト値にも影響します。

//emlis...
...t[][ruby]{
h = Hash.new([])
h[0] << 0
h[1] << 1
p h.default #=> [0, 1]
//}

これを避けるには、破壊的でないメソッドで再代入する必要が有ります。
また、このようなミスを防ぐためにもifnoneは freeze して破壊的操作を禁止しておくのが無難...
...//emlist[例][ruby]{
h = Hash.new([])

p h[1] #=> []
p h[1].object_id #=> 6127150
p h[1] << "bar" #=> ["bar"]
p h[1] #=> ["bar"]

p h[2] #=> ["bar"]
p h[2].object_id #=> 6127150

p h #=> {}


h = Hash.new([]...

Observable (6042.0)

Observer パターンを提供するモジュールです。

...ールです。

Mix-in により Observer パターンを提供します。

Observable モジュールを include したクラスは
Observable#changed メソッドにより更新フラグを立て、
Observable#notify_observers が呼び出されると
更新フラグが立っている場合は...
...servable#notify_observers の引数は
そのままオブザーバの update メソッドに渡されます。

=== サンプルコード
require "observer"

class Ticker ### Periodically fetch a stock price.
include Observable

def initialize(symbol)
@symbol = symbol...
...e below #@limit: #{price}\n"
end
end
end

class WarnHigh < Warner
def update(time, price) # callback for observer
if price > @limit
print "+++ #{time.to_s}: Price above #@limit: #{price}\n"
end
end
end

ticker = Ticker.new("MSFT")
WarnLow.new(tic...

絞り込み条件を変える

Data.define(*args) {|subclass| block } -> Class (266.0)

Data クラスに新しいサブクラスを作って、それを返します。

...れています。

//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred", 5)
p fred.name # => "Fred"
p fred.age # => 5
//}

メンバの値を書き換えることはできません。

//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred", 5)
fred.age = 6 #...
.../emlist[例][ruby]{
class HTTPFetcher
Response = Data.define(:body)
NotFound = Data.define

def get(url)
# ダミーの実装
if url == "http://example.com/"
Response.new(body: "Current time is #{Time.now}")
else
NotFound.new
end
end
end

def fetch(url)
fetch
er...
...= HTTPFetcher.new
case fetcher.get(url)
in HTTPFetcher::Response(body)
body
in HTTPFetcher::NotFound
:NotFound
end
end

p fetch("http://example.com/") # => "Current time is 2023-01-10 10:00:53 +0900"
p fetch("http://example.com/404") # => :NotFound
//}

@param args 値オブジ...

Data.define(*args) -> Class (166.0)

Data クラスに新しいサブクラスを作って、それを返します。

...れています。

//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred", 5)
p fred.name # => "Fred"
p fred.age # => 5
//}

メンバの値を書き換えることはできません。

//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred", 5)
fred.age = 6 #...
.../emlist[例][ruby]{
class HTTPFetcher
Response = Data.define(:body)
NotFound = Data.define

def get(url)
# ダミーの実装
if url == "http://example.com/"
Response.new(body: "Current time is #{Time.now}")
else
NotFound.new
end
end
end

def fetch(url)
fetch
er...
...= HTTPFetcher.new
case fetcher.get(url)
in HTTPFetcher::Response(body)
body
in HTTPFetcher::NotFound
:NotFound
end
end

p fetch("http://example.com/") # => "Current time is 2023-01-10 10:00:53 +0900"
p fetch("http://example.com/404") # => :NotFound
//}

@param args 値オブジ...

Hash#[](key) -> object | nil (130.0)

key に関連づけられた値を返します。

...il を区別する必要が
ある場合は Hash#fetch または Hash#key? を使ってください。

@param key 探索するキーを指定します。

//emlist[例][ruby]{
h = {:ab => "some" , :cd => "all"}
p h[:ab] #=> "some"
p h[:ef] #=> nil

h1 = Hash.new("default val...
...ue")
p h1[:non] #=> "default value"

h2 = Hash.new {|*arg| arg}
p h2[:non] #=> [{}, :non]
//}

@see Hash.new, Hash#fetch,Hash#values_at,Hash#key?, Hash#default, Hash#default_proc...

Thread#[](name) -> object | nil (130.0)

name に対応したスレッドに固有のデータを取り出します。 name に対応するスレッド固有データがなければ nil を返し ます。

...データがなければ nil を返し
ます。

@param name スレッド固有データのキーを文字列か Symbol で指定します。

//emlist[例][ruby]{
[
Thread.new { Thread.current["name"] = "A" },
Thread.new { Thread.current[:name] = "B" },
Thread.new { Thread.current["name"] =...
...と異なる変数を返す事に注意してください。

//emlist[][ruby]{
def meth(newvalue)
begin
oldvalue = Thread.current[:name]
Thread.current[:name] = newvalue
yield
ensure
Thread.current[:name] = oldvalue
end
end
//}

この関数に与えるブロックがFiberを...
...rrent[:name]
# => nil if fiber-local
# => 2 if thread-local (The value 2 is leaked to outside of meth method.)
//}

Fiber を切り替えても同じ変数を返したい場合は
Thread#thread_variable_get と Thread#thread_variable_set
を使用してください。

@see Thread#fetch, Thread#[]=...