summaryrefslogtreecommitdiffhomepage
path: root/mrblib
diff options
context:
space:
mode:
authortaiyoslime <[email protected]>2020-10-05 19:53:05 +0900
committertaiyoslime <[email protected]>2020-10-13 14:09:36 +0900
commitbec4d053400c3a11c8efd68c3e8bd5ea4a0bcc54 (patch)
tree166195f4009b90a90d1fe1b28ad4c8c84ba2597d /mrblib
parent9ea7b718683386d2dc0787e919fc3d413ab20e67 (diff)
downloadmruby-bec4d053400c3a11c8efd68c3e8bd5ea4a0bcc54.tar.gz
mruby-bec4d053400c3a11c8efd68c3e8bd5ea4a0bcc54.zip
Introduce endless range (a part of #5085)
Co-Authored-By: n4o847 <[email protected]> Co-Authored-By: smallkirby <[email protected]>
Diffstat (limited to 'mrblib')
-rw-r--r--mrblib/range.rb26
1 files changed, 24 insertions, 2 deletions
diff --git a/mrblib/range.rb b/mrblib/range.rb
index 392cc2274..704826459 100644
--- a/mrblib/range.rb
+++ b/mrblib/range.rb
@@ -12,8 +12,25 @@ class Range
def each(&block)
return to_enum :each unless block
- val = self.first
- last = self.last
+ val = self.begin
+ last = self.end
+
+ if val.kind_of?(Fixnum) && last.nil?
+ i = val
+ while true
+ block.call(i)
+ i += 1
+ end
+ return self
+ end
+
+ if val.kind_of?(String) && last.nil?
+ if val.respond_to? :__upto_endless
+ return val.__upto_endless(&block)
+ else
+ str_each = true
+ end
+ end
if val.kind_of?(Fixnum) && last.kind_of?(Fixnum) # fixnums are special
lim = last
@@ -56,6 +73,11 @@ class Range
h += 1 if self.exclude_end?
h
end
+
+ def to_a
+ raise RangeError, "cannot convert endless range to an array" if self.last.nil?
+ super
+ end
end
##