るりまサーチ

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

別のキーワード

  1. _builtin fetch
  2. dbm fetch
  3. env fetch
  4. array fetch
  5. net/http fetch

オブジェクト

キーワード

検索結果

<< < 1 2 >>

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

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

...Fetcher
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 fetch...
...er.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.new {|hash, key| ... } -> Hash (7.0)

空の新しいハッシュを生成します。ブロックの評価結果がデフォルト値になりま す。設定したデフォルト値はHash#default_procで参照できます。

...#=> "foo"
p h[2].object_id #=> 6126840

p h #=> {1=>"foobar", 2=>"foo"}

# 値が設定されていないときに(fetchのように)例外をあげるようにもできる
h = Hash.new {|hash, key|
raise(IndexError, "hash[#{key}] has no value")...

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

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

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

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

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

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

これを避けるには、破壊的でないメソッドで再代入する...
<< < 1 2 >>