From 9b97805e63621c06825b4a7394d907d0e60eb59f Mon Sep 17 00:00:00 2001 From: Kouhei Yanagita Date: Sun, 8 Dec 2024 12:26:15 +0900 Subject: [PATCH] =?UTF-8?q?Ruby=203.4=E3=81=A7Range#step=E3=81=8C=20+=20?= =?UTF-8?q?=E3=82=92=E4=BD=BF=E7=94=A8=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=AA=E3=81=A3=E3=81=9F=E3=81=93=E3=81=A8=E3=81=AB?= =?UTF-8?q?=E9=96=A2=E3=81=99=E3=82=8B=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- refm/api/src/_builtin/Range | 47 ++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/refm/api/src/_builtin/Range b/refm/api/src/_builtin/Range index 73c9cd7ee6..1ee2c863f0 100644 --- a/refm/api/src/_builtin/Range +++ b/refm/api/src/_builtin/Range @@ -523,23 +523,54 @@ Range#each は各要素の succ メソッドを使用してイテレーション 範囲内の要素を s おきに繰り返します。 -#@since 2.6.0 +#@since 3.4 +@param s 次のステップへ遷移するたびに加算されるものを指定します。 +#@else @param s 各ステップの大きさを数値で指定します。負の数を指定することもできます。 +#@end @return ブロックを指定した時は self を返します。 @return ブロックを指定しなかった時かつ数値の Range の時は [[c:Enumerator::ArithmeticSequence]] を返します。 @return ブロックを指定しなかったその他の Range の時は [[c:Enumerator]] を返します。(例: String の Range) -#@else -@param s 正の整数を指定します。 -@return ブロックつきの時は self を返します。 -@return ブロックなしの時は [[c:Enumerator]] を返します。 -@raise ArgumentError s に 0 または負の数を指定した場合に発生します -#@end #@samplecode 例 -("a" .. "f").step(2) {|v| p v} +(1..10).step(3) {|v| p v} +# => 1 +# 4 +# 7 +# 10 + +("a".."f").step(2) {|v| p v} # => "a" # "c" # "e" + +(10..0).step(-3) {|v| p v} +# => 10 +# 7 +# 4 +# 1 +#@end + +#@since 3.4 + +非数値の Range では、イテレーションに「要素 + s」を使用します。 +(文字列またはシンボルの Range で s に数値を指定した場合を除きます) + +#@samplecode 数値以外の Range に対する例 +# Time の Range は each でイテレートできない +(Time.utc(2024, 12, 25)...Time.utc(2024, 12, 26)).each { |t| p t } +# => 'Range#each': can't iterate from Time (TypeError) + +# step は使用可能 +(Time.utc(2024, 12, 25)...Time.utc(2024, 12, 26)).step(60*60*6) { |t| p t } +# => 2024-12-25 00:00:00 UTC +# 2024-12-25 06:00:00 UTC +# 2024-12-25 12:00:00 UTC +# 2024-12-25 18:00:00 UTC + +("a"..).step("*").take(3) # => ["a", "a*", "a**"] +#@end + #@end --- ==(other) -> bool