[ruby-core:62822] [ruby-trunk - Feature #9867] Introduce each/else block into ruby

From: sawadatsuyoshi@...
Date: 2014-05-28 13:54:02 UTC
List: ruby-core #62822
Issue #9867 has been updated by Tsuyoshi Sawada.


Tsuyoshi Sawada wrote:
> Łukasz Strzebińczyk wrote:
> 
> > ... the 'else' block is invoked when collection is empty.
> 
> Your proposal is about `any?`, but your explanation mentions emptiness, or `empty?`, and so the usecase and the proposal do not match. Are you confusing `any?` with negation of `empty?`? In some cases, the former can be a shorthand for the latter, but they are different.
> 
> If your real intention was to condition the first part by negation of `empty?` rather than `any?`, then you don't need to condition for the first part as empty array will have nothing to iterate over. It can be simply written as:
> 
>     array.each do |elem|
>       some_work
>     end
>     unless array.any?
>       do_something_else
>     end



----------------------------------------
Feature #9867: Introduce each/else block into ruby
https://bugs.ruby-lang.org/issues/9867#change-46946

* Author: Łukasz Strzebińczyk
* Status: Feedback
* Priority: Low
* Assignee: Yukihiro Matsumoto
* Category: syntax
* Target version: 
----------------------------------------
Hi

Code like this happens quite often:

    if array.any?
      array.each do |elem|
        some_work
      end
    else
      do_something_else
    end

I was thinking if it was possible, to introduce syntax like this:

    array.each do |elem|
      some_work
    else
      do_something_else
    end

where code in else would fire if array is empty. This would simplify a lot of code nicely and would be completely backwards compatible. Is that a good idea?



-- 
https://bugs.ruby-lang.org/

In This Thread

Prev Next