Merge pull request #519 from dmitryilyin/fetch

[MAINT] Improve 'try_get_value' readme
This commit is contained in:
TP Honey 2015-09-08 11:15:45 +01:00
commit 00b11c20c8

View file

@ -706,12 +706,18 @@ Converts the argument into bytes, for example "4 kB" becomes "4096". Takes a sin
*Type*: rvalue. *Type*: rvalue.
Looks up into a complex structure of arrays and hashes and returns a value Looks up into a complex structure of arrays and hashes to extract a value by
or the default value if nothing was found. its path in the structure. The path is a string of hash keys or array indexes
starting with zero, separated by the path separator character (default "/").
The function will go down the structure by each path component and will try to
return the value at the end of the path.
Key can contain slashes to describe path components. The function will go down In addition to the required "path" argument the function accepts the default
the structure and try to extract the required value. argument. It will be returned if the path is not correct, no value was found or
a any other error have occurred. And the last argument can set the path
separator character.
```ruby
$data = { $data = {
'a' => { 'a' => {
'b' => [ 'b' => [
@ -722,19 +728,28 @@ $data = {
} }
} }
$value = try_get_value($data, 'a/b/2')
# $value = 'b3'
# with all possible options
$value = try_get_value($data, 'a/b/2', 'not_found', '/') $value = try_get_value($data, 'a/b/2', 'not_found', '/')
=> $value = 'b3' # $value = 'b3'
a -> first hash key # using the default value
b -> second hash key $value = try_get_value($data, 'a/b/c/d', 'not_found')
2 -> array index starting with 0 # $value = 'not_found'
not_found -> (optional) will be returned if there is no value or the path did not match. Defaults to nil. # using custom separator
/ -> (optional) path delimiter. Defaults to '/'. $value = try_get_value($data, 'a|b', [], '|')
# $value = ['b1','b2','b3']
```
In addition to the required "key" argument, "try_get_value" accepts default 1. **$data** The data structure we are working with.
argument. It will be returned if no value was found or a path component is 2. **'a/b/2'** The path string.
missing. And the fourth argument can set a variable path separator. 3. **'not_found'** The default value. It will be returned if nothing is found.
(optional, defaults to *undef*)
4. **'/'** The path separator character.
(optional, defaults to *'/'*)
#### `type3x` #### `type3x`