[#66126] Creation/Conversion methods/functions table for Ruby types — SASADA Koichi <ko1@...>
Hi,
5 messages
2014/11/07
[#66248] [ruby-trunk - Feature #10423] [PATCH] opt_str_lit*: avoid literal string allocations — normalperson@...
Issue #10423 has been updated by Eric Wong.
3 messages
2014/11/13
[#66595] [ruby-trunk - Bug #10557] [Open] Block not given when the argument is a string — bartosz@...
Issue #10557 has been reported by Bartosz Kopinski.
3 messages
2014/11/30
[ruby-core:66316] [ruby-trunk - Feature #10498] Make `loop` yield a counter
From:
funny.falcon@...
Date:
2014-11-16 10:52:43 UTC
List:
ruby-core #66316
Issue #10498 has been updated by Yura Sokolov.
~~~
> LOOP = 2**1000
=> 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
> LOOP.times.take(10)
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> LOOP.times{|i| p i; break if i == 2}
0
1
2
=> nil
~~~
But still, if `loop` yields index it will not damage anyone.
----------------------------------------
Feature #10498: Make `loop` yield a counter
https://bugs.ruby-lang.org/issues/10498#change-49977
* Author: Franck Verrot
* Status: Open
* Priority: Normal
* Assignee: ruby-core
* Category: core
* Target version: current: 2.2.0
----------------------------------------
# Problem
Teaching Ruby, we always end up with that type of construct
```ruby
i = 0
loop do
i += 1
# do something with i....
raise StopIteration if i ...
end
```
# Solution
What I propose with this patch is making `loop` yield the iteration count:
```ruby
loop do |i|
# do something with i....
raise StopIteration if i ...
end
```
`i` starts at 0 and stops at `FIXNUM_MAX` (there's no `Float::Infinity` equivalent for integers).
# Alternate solution
`Integer#times` could work if we had an `<Integer's infinity>` object, so we would just do `<Integer's Infinity>.times { |i| ... }`.
Also, this is the very first patch I submit to Ruby, I might have done something horrible, feel free to tell me :-)
---Files--------------------------------
0001-vm_eval.c-loop-now-yields-a-incremented-counter.patch (1.74 KB)
0001-vm_eval.c-loop-now-yields-a-incremented-counter.patch (1.86 KB)
--
https://bugs.ruby-lang.org/