summaryrefslogtreecommitdiff
path: root/README.markdown
diff options
context:
space:
mode:
authorMaksym Melnychok <maksym@yelp.com>2016-02-08 07:50:35 -0800
committerMaksym Melnychok <maksym@yelp.com>2016-02-16 00:51:28 -0800
commit3169a43f4c24d01c64c90ab9537da284f587c726 (patch)
tree8ce7f17067cb5df0e75784f400d3ccc1c7c6f505 /README.markdown
parent990e1d757549a9c792cf5f7113e4d6bcd592ae3d (diff)
Add dig() function
Deprecates #try_get_value()
Diffstat (limited to 'README.markdown')
-rw-r--r--README.markdown38
1 files changed, 38 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index 559a6a0..c2d8388 100644
--- a/README.markdown
+++ b/README.markdown
@@ -254,6 +254,42 @@ Deletes all instances of the undef value from an array or hash. For example, `$h
Returns the difference between two arrays. The returned array is a copy of the original array, removing any items that also appear in the second array. For example, `difference(["a","b","c"],["b","c","d"])` returns ["a"]. *Type*: rvalue.
+#### `dig`
+
+*Type*: rvalue.
+
+Retrieves a value within multiple layers of hashes and arrays via an array of keys containing a path. The function goes through the structure by each path component and tries to return the value at the end of the path.
+
+In addition to the required path argument, the function accepts the default argument. It is returned if the path is not correct, if no value was found, or if any other error has occurred.
+
+~~~ruby
+$data = {
+ 'a' => {
+ 'b' => [
+ 'b1',
+ 'b2',
+ 'b3',
+ ]
+ }
+}
+
+$value = dig($data, ['a', 'b', 2])
+# $value = 'b3'
+
+# with all possible options
+$value = dig($data, ['a', 'b', 2], 'not_found')
+# $value = 'b3'
+
+# using the default value
+$value = dig($data, ['a', 'b', 'c', 'd'], 'not_found')
+# $value = 'not_found'
+~~~
+
+1. **$data** The data structure we are working with.
+2. **['a', 'b', 2]** The path array.
+3. **'not_found'** The default value. It will be returned if nothing is found.
+ (optional, defaults to *undef*)
+
#### `dirname`
Returns the `dirname` of a path. For example, `dirname('/path/to/a/file.ext')` returns '/path/to/a'. *Type*: rvalue.
@@ -806,6 +842,8 @@ Converts the argument into bytes, for example "4 kB" becomes "4096". Takes a sin
*Type*: rvalue.
+DEPRECATED: replaced by `dig()`.
+
Retrieves a value within multiple layers of hashes and arrays via a string containing a path. The path is a string of hash keys or array indexes starting with zero, separated by the path separator character (default "/"). The function goes through the structure by each path component and tries to return the value at the end of the path.
In addition to the required path argument, the function accepts the default argument. It is returned if the path is not correct, if no value was found, or if any other error has occurred. The last argument can set the path separator character.