Issue #13807 has been updated by nobu (Nobuyoshi Nakada).


It seems useless without method chain, i.e., variable `a` is too simple as an example.

That is, this is a variant of `yield_self`.

```ruby
foo.bar.zot.yield_self {|a| a.some_condition ? a : b}
```

or

```ruby
foo.bar.zot.verify {|a| a.some_condition} || b
foo.bar.zot.verify(&:some_condition) || b
```

But `verify` is long and sounds ambiguous.

This came to my mind, but looks magical a little.

```ruby
foo.bar.zot.if?(&:some_condition) || b
```


----------------------------------------
Feature #13807: A method to filter the receiver against some condition
https://bugs.ruby-lang.org/issues/13807#change-66158

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I frequently see code that uses some value if that value satisfies a certain condition, and something else otherwise.

```ruby
a.some_condition ? a : b
```

And in most cases, the value of `a` is non-nil when the condition is satisfied.

I propose to have a method, perhaps named `verify`, which would implemented to be equivalent to this definition:

```ruby
class Object
  def verify
    self if yield(self)
  end
end
```

Then, we can write the expression above (assuming `a` is non-nil when the condition is satisfied) like this:

```ruby
a.verify{|a| a.some_condition} || b
```

Perhaps it would also be useful to do something like:

```ruby
a.verify{|a| a.some_condition}&.chaining_of_more_methods
```



-- 
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>