Issue #13290 has been updated by Robert A. Heiler.


I love case/when menus, so I am all for suggestions that make case/when more
useful, so I also support this. I also would like to see case/when become
first-class citizens (objects) one day that can also be merged together
(e. g. unify two different case/when menus together, both working on
the same variable... a bit similar as to how hashes can be merged 
together too. A bit like UnboundMethod where we could, in theory,
detach a method and attach it to another object or use another 
environment ... but I digress again, sorry.)

The name "fetch_as_in_case_construction" is indeed a bit terrible,
I am glad that I am not the only one who suggests terrible names. :D

But I think the more important part is whether matz/ruby core team
think that the underlying functionality would be ok to be had.

Finding good names is hard but possible! I also have no really better
suggestion. My suggestions may even be worse ... such as :)

Obvious ones would be ".to_case" or, if the other way around, on
a first-class citizen case object, .to_hash. Or some method called
"convert" or "transform" or "treat_as_case" or ... I dunno.

But +1 anyway.

----------------------------------------
Feature #13290: A method to use a hash like in a case construction
https://bugs.ruby-lang.org/issues/13290#change-63385

* Author: Tsuyoshi Sawada
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
We often want to translate a hash into a case construction and back. For example, suppose we have a case construction like this:

```ruby
case key
when /foo/ then "FOO"
when /bar/ then "BAR"
else "DEFAULT"
end
```

Given that the keys are ordered within a hash since Ruby 1.9, the information used in the case construction above can be expressed as a hash `h`:

```ruby
h = {
  /foo/ => "FOO"
  /bar/ => "BAR"
  ...
}
h.default = "DEFAULT"
```

At the moment, there is no straightforward way to translate the hash `h` into the case construction as above. I would like to have a method on `Hash` that takes a key and works like a case construction (i.e., applies the `===` method) as below:

```ruby
h.fetch_as_in_case_construction("barbar") # => "BAR"
```

The name `fetch_as_in_case_construction` is terrible. Perhaps someone can suggest a better name.



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