ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-dev:50916] [Ruby master Feature#13067] TrueClass, FalseClass to provide `===` to match truthy/falsy values.
       [not found] <redmine.issue-13067.20161224170820.24242@ruby-lang.org>
@ 2020-02-06 21:24 ` jonathan
  0 siblings, 0 replies; only message in thread
From: jonathan @ 2020-02-06 21:24 UTC (permalink / raw)
  To: ruby-dev

Issue #13067 has been updated by jonathanhefner (Jonathan Hefner).


Although I like the minimalism and cleverness of `:itself.to_proc`, I don't think it reads as nicely.  It also does not `inspect` nicely.  For example,

```ruby
raise "value does not match #{pattern.inspect}" unless pattern === value
```

Does not give a nice error message when `pattern` is `:itself.to_proc`.  

Also, when you specifically want to match falsy values, `:itself.to_proc` must become `:!.to_proc`, which may not be obvious.

So, I think constants like `TRUTHY` and `FALSEY` would be helpful.  Although they might not be appropriate as top-level constants.  While trying to think of where they should be defined, I had the crazy idea of `TrueClass#thy` and `FalseClass#y`.  In other words, `true.thy == TRUTHY` and `false.y == FALSEY`.  That is too asymmetric and gimmicky, I think, but what about `true.ish == TRUTHY` and `false.ish == FALSEY`?


----------------------------------------
Feature #13067: TrueClass,FalseClass to provide `===` to match truthy/falsy values.
https://bugs.ruby-lang.org/issues/13067#change-84185

* Author: matz (Yukihiro Matsumoto)
* Status: Closed
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
I propose to make `TrueClass`, `FalseClass` to provide `===` method to match truthy values (`TrueClass`), and falsy values (`FalseClass`), so that we can use true and false for case pattern matching.  And we can pick truthy values using `grep` e.g. `ary.grep(true)`.

Matz.



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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-02-06 21:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-13067.20161224170820.24242@ruby-lang.org>
2020-02-06 21:24 ` [ruby-dev:50916] [Ruby master Feature#13067] TrueClass, FalseClass to provide `===` to match truthy/falsy values jonathan

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 NNTP newsgroup(s).