[#73707] [Ruby trunk Misc#12004] Code of Conduct — hanmac@...
Issue #12004 has been updated by Hans Mackowiak.
3 messages
2016/02/05
[#73730] [Ruby trunk Feature#12034] RegExp does not respect file encoding directive — nobu@...
Issue #12034 has been updated by Nobuyoshi Nakada.
3 messages
2016/02/07
[#73746] [Ruby trunk Feature#12034] RegExp does not respect file encoding directive — nobu@...
Issue #12034 has been updated by Nobuyoshi Nakada.
3 messages
2016/02/09
[#73919] [Ruby trunk Feature#11262] Make more objects behave like "Functions" — Ruby-Lang@...
Issue #11262 has been updated by J旦rg W Mittag.
3 messages
2016/02/22
[#74019] [Ruby trunk Bug#12103][Rejected] ruby process hangs while executing regular expression. — duerst@...
Issue #12103 has been updated by Martin D端rst.
3 messages
2016/02/27
[ruby-core:73862] [Ruby trunk Feature#12080] Enumerable#first, Array#last with block
From:
k@...
Date:
2016-02-17 13:38:20 UTC
List:
ruby-core #73862
Issue #12080 has been reported by Kazuki Yamaguchi.
----------------------------------------
Feature #12080: Enumerable#first, Array#last with block
https://bugs.ruby-lang.org/issues/12080
* Author: Kazuki Yamaguchi
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
I propose a new feature: {Enumerable,Array,Range}#first, {Array,Range}#last with block.
If no argument is passed:
* `enum.first { block }` works as the same as `enum.find { block }`
* `ary.last { block }` works as the same as `ary.reverse_each.find { block }`
If a integer is passed:
* `enum.first(n) { block }` works as the same as `enum.lazy.select { block }.first(n)`
* `ary.last(n) { block }` works as the same as `ary.reverse_each.lazy.select { block }.first(n).reverse`
Examples:
~~~ruby
ary = [1, 2, 3, 4, 5, 6]
ary.first { |i| i.even? } #=> 2
ary.first(2) { |i| i.even? } #=> [2, 4]
ary.last { |i| i.odd? } #=> 5
ary.last(2) { |i| i.odd? } #=> [3, 5]
~~~
Currently these methods just ignore given blocks.
~~~ruby
ary.first { |i| i > 3 } #=> 1
~~~
---
I sometimes write such code:
~~~ruby
ary = ["DEBUG: a", "WARN: b", ..., "WARN: y", "DEBUG: z"] # assume a large Array
ary.reverse_each.lazy.reject { |line| line.include?("DEBUG") }.first(10).reverse #=> returns last 10 non-debug logs
~~~
But this is redundant and not intuitive. In this case, I just want the last 10 logs which is not debug message in the original order.
With the new Array#last, I can refactor it:
~~~ruby
ary.last(10) { |line| !line.include?("DEBUG") }
~~~
I think this represents what I want to do much more clearly.
---Files--------------------------------
0001-Enumerable-Array-Range-first-Array-Range-last-with-b.patch (11.5 KB)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>