* [ruby-core:119614] [Ruby master Feature#20815] Fetch for nested hash
@ 2024-10-25 11:48 dmytrostrukov (Dmytro Strukov) via ruby-core
2024-10-25 13:46 ` [ruby-core:119615] " matheusrich (Matheus Richard) via ruby-core
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: dmytrostrukov (Dmytro Strukov) via ruby-core @ 2024-10-25 11:48 UTC (permalink / raw)
To: ruby-core; +Cc: dmytrostrukov (Dmytro Strukov)
Issue #20815 has been reported by dmytrostrukov (Dmytro Strukov).
----------------------------------------
Feature #20815: Fetch for nested hash
https://bugs.ruby-lang.org/issues/20815
* Author: dmytrostrukov (Dmytro Strukov)
* Status: Open
----------------------------------------
I propose adding a `fetch_nested` method to Ruby’s Hash class, allowing developers to fetch values deeply nested within a hash, with an error raised if any key in the provided path is missing. The method would be similar to dig, but with the added functionality of error handling when keys are not found, improving code readability and robustness when accessing nested data structures.
**Rationale**
- Enhanced Error Handling: `fetch_nested` would raise an error if a key in the specified path does not exist, which is critical for cases where the absence of specific keys indicates a data integrity issue or invalid structure. This makes debugging easier and helps catch issues early in the development process.
- Improved Code Readability: Without `fetch_nested`, developers often chain multiple fetch calls or rely on error-prone conditional checks, complicating code. A single `fetch_nested` method call would simplify syntax and improve readability when working with deeply nested hashes.
- Alignment with Ruby’s Error-First Philosophy: Ruby’s fetch method already offers error handling for missing keys at the top level of a hash. Extending this philosophy to nested hashes would offer consistency and convenience, aligning with Ruby’s emphasis on developer-friendly and expressive error handling.
**Example Usage**
``` ruby
data = { user: { profile: { name: "Alice" } } }
# Current approach
data.fetch(:user).fetch(:profile).fetch(:name) # Or use dig without error handling
# Proposed `fetch_nested`
data.fetch_nested(:user, :profile, :name) # Raises KeyError if any key is missing
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
* [ruby-core:119615] [Ruby master Feature#20815] Fetch for nested hash
2024-10-25 11:48 [ruby-core:119614] [Ruby master Feature#20815] Fetch for nested hash dmytrostrukov (Dmytro Strukov) via ruby-core
@ 2024-10-25 13:46 ` matheusrich (Matheus Richard) via ruby-core
2024-10-25 14:32 ` [ruby-core:119618] " nobu (Nobuyoshi Nakada) via ruby-core
2024-10-26 16:32 ` [ruby-core:119630] " byroot (Jean Boussier) via ruby-core
2 siblings, 0 replies; 4+ messages in thread
From: matheusrich (Matheus Richard) via ruby-core @ 2024-10-25 13:46 UTC (permalink / raw)
To: ruby-core; +Cc: matheusrich (Matheus Richard)
Issue #20815 has been updated by matheusrich (Matheus Richard).
I think this might have been suggested in the past as `deep_fetch`. Could it be `dig!` maybe?
----------------------------------------
Feature #20815: Fetch for nested hash
https://bugs.ruby-lang.org/issues/20815#change-110235
* Author: dmytrostrukov (Dmytro Strukov)
* Status: Open
----------------------------------------
I propose adding a `fetch_nested` method to Ruby’s Hash class, allowing developers to fetch values deeply nested within a hash, with an error raised if any key in the provided path is missing. The method would be similar to dig, but with the added functionality of error handling when keys are not found, improving code readability and robustness when accessing nested data structures.
**Rationale**
- Enhanced Error Handling: `fetch_nested` would raise an error if a key in the specified path does not exist, which is critical for cases where the absence of specific keys indicates a data integrity issue or invalid structure. This makes debugging easier and helps catch issues early in the development process.
- Improved Code Readability: Without `fetch_nested`, developers often chain multiple fetch calls or rely on error-prone conditional checks, complicating code. A single `fetch_nested` method call would simplify syntax and improve readability when working with deeply nested hashes.
- Alignment with Ruby’s Error-First Philosophy: Ruby’s fetch method already offers error handling for missing keys at the top level of a hash. Extending this philosophy to nested hashes would offer consistency and convenience, aligning with Ruby’s emphasis on developer-friendly and expressive error handling.
**Example Usage**
``` ruby
data = { user: { profile: { name: "Alice" } } }
# Current approach
data.fetch(:user).fetch(:profile).fetch(:name) # Or use dig without error handling
# Proposed `fetch_nested`
data.fetch_nested(:user, :profile, :name) # Raises KeyError if any key is missing
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
* [ruby-core:119618] [Ruby master Feature#20815] Fetch for nested hash
2024-10-25 11:48 [ruby-core:119614] [Ruby master Feature#20815] Fetch for nested hash dmytrostrukov (Dmytro Strukov) via ruby-core
2024-10-25 13:46 ` [ruby-core:119615] " matheusrich (Matheus Richard) via ruby-core
@ 2024-10-25 14:32 ` nobu (Nobuyoshi Nakada) via ruby-core
2024-10-26 16:32 ` [ruby-core:119630] " byroot (Jean Boussier) via ruby-core
2 siblings, 0 replies; 4+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2024-10-25 14:32 UTC (permalink / raw)
To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)
Issue #20815 has been updated by nobu (Nobuyoshi Nakada).
```ruby
data => user: {profile: {name:}}
p name #=> "Alice"
data => user: {profile: {age:}} #=> NoMatchingPatternKeyError
```
----------------------------------------
Feature #20815: Fetch for nested hash
https://bugs.ruby-lang.org/issues/20815#change-110240
* Author: dmytrostrukov (Dmytro Strukov)
* Status: Open
----------------------------------------
I propose adding a `fetch_nested` method to Ruby’s Hash class, allowing developers to fetch values deeply nested within a hash, with an error raised if any key in the provided path is missing. The method would be similar to dig, but with the added functionality of error handling when keys are not found, improving code readability and robustness when accessing nested data structures.
**Rationale**
- Enhanced Error Handling: `fetch_nested` would raise an error if a key in the specified path does not exist, which is critical for cases where the absence of specific keys indicates a data integrity issue or invalid structure. This makes debugging easier and helps catch issues early in the development process.
- Improved Code Readability: Without `fetch_nested`, developers often chain multiple fetch calls or rely on error-prone conditional checks, complicating code. A single `fetch_nested` method call would simplify syntax and improve readability when working with deeply nested hashes.
- Alignment with Ruby’s Error-First Philosophy: Ruby’s fetch method already offers error handling for missing keys at the top level of a hash. Extending this philosophy to nested hashes would offer consistency and convenience, aligning with Ruby’s emphasis on developer-friendly and expressive error handling.
**Example Usage**
``` ruby
data = { user: { profile: { name: "Alice" } } }
# Current approach
data.fetch(:user).fetch(:profile).fetch(:name) # Or use dig without error handling
# Proposed `fetch_nested`
data.fetch_nested(:user, :profile, :name) # Raises KeyError if any key is missing
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
* [ruby-core:119630] [Ruby master Feature#20815] Fetch for nested hash
2024-10-25 11:48 [ruby-core:119614] [Ruby master Feature#20815] Fetch for nested hash dmytrostrukov (Dmytro Strukov) via ruby-core
2024-10-25 13:46 ` [ruby-core:119615] " matheusrich (Matheus Richard) via ruby-core
2024-10-25 14:32 ` [ruby-core:119618] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2024-10-26 16:32 ` byroot (Jean Boussier) via ruby-core
2 siblings, 0 replies; 4+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-10-26 16:32 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #20815 has been updated by byroot (Jean Boussier).
Status changed from Open to Closed
Closing because it's a duplicate.
----------------------------------------
Feature #20815: Fetch for nested hash
https://bugs.ruby-lang.org/issues/20815#change-110252
* Author: dmytrostrukov (Dmytro Strukov)
* Status: Closed
----------------------------------------
I propose adding a `fetch_nested` method to Ruby’s Hash class, allowing developers to fetch values deeply nested within a hash, with an error raised if any key in the provided path is missing. The method would be similar to dig, but with the added functionality of error handling when keys are not found, improving code readability and robustness when accessing nested data structures.
**Rationale**
- Enhanced Error Handling: `fetch_nested` would raise an error if a key in the specified path does not exist, which is critical for cases where the absence of specific keys indicates a data integrity issue or invalid structure. This makes debugging easier and helps catch issues early in the development process.
- Improved Code Readability: Without `fetch_nested`, developers often chain multiple fetch calls or rely on error-prone conditional checks, complicating code. A single `fetch_nested` method call would simplify syntax and improve readability when working with deeply nested hashes.
- Alignment with Ruby’s Error-First Philosophy: Ruby’s fetch method already offers error handling for missing keys at the top level of a hash. Extending this philosophy to nested hashes would offer consistency and convenience, aligning with Ruby’s emphasis on developer-friendly and expressive error handling.
**Example Usage**
``` ruby
data = { user: { profile: { name: "Alice" } } }
# Current approach
data.fetch(:user).fetch(:profile).fetch(:name) # Or use dig without error handling
# Proposed `fetch_nested`
data.fetch_nested(:user, :profile, :name) # Raises KeyError if any key is missing
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-10-26 16:33 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-25 11:48 [ruby-core:119614] [Ruby master Feature#20815] Fetch for nested hash dmytrostrukov (Dmytro Strukov) via ruby-core
2024-10-25 13:46 ` [ruby-core:119615] " matheusrich (Matheus Richard) via ruby-core
2024-10-25 14:32 ` [ruby-core:119618] " nobu (Nobuyoshi Nakada) via ruby-core
2024-10-26 16:32 ` [ruby-core:119630] " byroot (Jean Boussier) via ruby-core
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).