@@ -706,12 +706,18 @@ Converts the argument into bytes, for example "4 kB" becomes "4096". Takes a sin
706706
707707* Type* : rvalue.
708708
709- Looks up into a complex structure of arrays and hashes and returns a value
710- or the default value if nothing was found.
711-
712- Key can contain slashes to describe path components. The function will go down
713- the structure and try to extract the required value.
714-
709+ Looks up into a complex structure of arrays and hashes to extract a value by
710+ its path in the structure. The path is a string of hash keys or array indexes
711+ starting with zero, separated by the path separator character (default "/").
712+ The function will go down the structure by each path component and will try to
713+ return the value at the end of the path.
714+
715+ In addition to the required "path" argument the function accepts the default
716+ argument. It will be returned if the path is not correct, no value was found or
717+ a any other error have occurred. And the last argument can set the path
718+ separator character.
719+
720+ ``` ruby
715721$data = {
716722 ' a' => {
717723 ' b' => [
@@ -722,19 +728,28 @@ $data = {
722728 }
723729}
724730
725- $value = try_get_value($data, 'a/b/2', 'not_found', '/')
726- => $value = 'b3'
727-
728- a -> first hash key
729- b -> second hash key
730- 2 -> array index starting with 0
731-
732- not_found -> (optional) will be returned if there is no value or the path did not match. Defaults to nil.
733- / -> (optional) path delimiter. Defaults to '/'.
731+ $value = try_get_value($data , ' a/b/2' )
732+ # $value = 'b3'
734733
735- In addition to the required "key" argument, "try_get_value" accepts default
736- argument. It will be returned if no value was found or a path component is
737- missing. And the fourth argument can set a variable path separator.
734+ # with all possible options
735+ $value = try_get_value($data , ' a/b/2' , ' not_found' , ' /' )
736+ # $value = 'b3'
737+
738+ # using the default value
739+ $value = try_get_value($data , ' a/b/c/d' , ' not_found' )
740+ # $value = 'not_found'
741+
742+ # using custom separator
743+ $value = try_get_value($data , ' a|b' , [], ' |' )
744+ # $value = ['b1','b2','b3']
745+ ```
746+
747+ 1 . ** $data** The data structure we are working with.
748+ 2 . ** 'a/b/2'** The path string.
749+ 3 . ** 'not_found'** The default value. It will be returned if nothing is found.
750+ (optional, defaults to * undef* )
751+ 4 . ** '/'** The path separator character.
752+ (optional, defaults to * '/'* )
738753
739754#### ` type3x `
740755
0 commit comments