ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:82353] [Ruby trunk Feature#13807] A method to filter the receiver against some condition
       [not found] <redmine.issue-13807.20170811203809@ruby-lang.org>
@ 2017-08-11 20:38 ` sawadatsuyoshi
  2017-08-12  4:01 ` [ruby-core:82357] " shevegen
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: sawadatsuyoshi @ 2017-08-11 20:38 UTC (permalink / raw)
  To: ruby-core

Issue #13807 has been reported by sawa (Tsuyoshi Sawada).

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

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:82357] [Ruby trunk Feature#13807] A method to filter the receiver against some condition
       [not found] <redmine.issue-13807.20170811203809@ruby-lang.org>
  2017-08-11 20:38 ` [ruby-core:82353] [Ruby trunk Feature#13807] A method to filter the receiver against some condition sawadatsuyoshi
@ 2017-08-12  4:01 ` shevegen
  2017-08-12 10:05 ` [ruby-core:82360] " duerst
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: shevegen @ 2017-08-12  4:01 UTC (permalink / raw)
  To: ruby-core

Issue #13807 has been updated by shevegen (Robert A. Heiler).


I have no particular pro or con opinion about your proposal, but I just
want to say that the last variant is not very pretty.

The lonely guy operator staring at the dot before him is pressed hard
against the wall behind him there, the "}" character!

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

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:82360] [Ruby trunk Feature#13807] A method to filter the receiver against some condition
       [not found] <redmine.issue-13807.20170811203809@ruby-lang.org>
  2017-08-11 20:38 ` [ruby-core:82353] [Ruby trunk Feature#13807] A method to filter the receiver against some condition sawadatsuyoshi
  2017-08-12  4:01 ` [ruby-core:82357] " shevegen
@ 2017-08-12 10:05 ` duerst
  2017-08-12 10:32 ` [ruby-core:82361] " nobu
  2019-01-23  9:18 ` [ruby-core:91221] " sawadatsuyoshi
  4 siblings, 0 replies; 5+ messages in thread
From: duerst @ 2017-08-12 10:05 UTC (permalink / raw)
  To: ruby-core

Issue #13807 has been updated by duerst (Martin Dürst).


I don't see any improvement. The new way that would be possible with the `verify` method is longer and more complicated than the simple and straightforward `a.some_condition ? a : b`.

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

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:82361] [Ruby trunk Feature#13807] A method to filter the receiver against some condition
       [not found] <redmine.issue-13807.20170811203809@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2017-08-12 10:05 ` [ruby-core:82360] " duerst
@ 2017-08-12 10:32 ` nobu
  2019-01-23  9:18 ` [ruby-core:91221] " sawadatsuyoshi
  4 siblings, 0 replies; 5+ messages in thread
From: nobu @ 2017-08-12 10:32 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:91221] [Ruby trunk Feature#13807] A method to filter the receiver against some condition
       [not found] <redmine.issue-13807.20170811203809@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2017-08-12 10:32 ` [ruby-core:82361] " nobu
@ 2019-01-23  9:18 ` sawadatsuyoshi
  4 siblings, 0 replies; 5+ messages in thread
From: sawadatsuyoshi @ 2019-01-23  9:18 UTC (permalink / raw)
  To: ruby-core

Issue #13807 has been updated by sawa (Tsuyoshi Sawada).


I came up with an idea better than this (https://bugs.ruby-lang.org/issues/15557).

So I withdraw this proposal. Please close it.

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

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-01-23  9:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-13807.20170811203809@ruby-lang.org>
2017-08-11 20:38 ` [ruby-core:82353] [Ruby trunk Feature#13807] A method to filter the receiver against some condition sawadatsuyoshi
2017-08-12  4:01 ` [ruby-core:82357] " shevegen
2017-08-12 10:05 ` [ruby-core:82360] " duerst
2017-08-12 10:32 ` [ruby-core:82361] " nobu
2019-01-23  9:18 ` [ruby-core:91221] " sawadatsuyoshi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).