Project

General

Profile

Actions

Feature #13807

closed

A method to filter the receiver against some condition

Added by sawa (Tsuyoshi Sawada) over 7 years ago. Updated almost 6 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:82353]

Description

I frequently see code that uses some value if that value satisfies a certain condition, and something else otherwise.

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:

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:

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

Perhaps it would also be useful to do something like:

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

Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #15557: A new class that stores a condition and the previous receiverOpenActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0