Sha256: e0602e69d68f01c03c578a989b4cc120e666410ab4ccb1fa5972d68f12890c4b

Contents?: true

Size: 1.2 KB

Versions: 116

Compression:

Stored size: 1.2 KB

Contents

Puppet::Parser::Functions::newfunction(
  :lest,
  :type => :rvalue,
  :arity => -2,
  :doc => <<-DOC
Call a [lambda](https://docs.puppet.com/puppet/latest/reference/lang_lambdas.html)
(which should accept no arguments) if the argument given to the function is `undef`.
Returns the result of calling the lambda if the argument is `undef`, otherwise the
given argument.

The `lest` function is useful in a chain of `then` calls, or in general
as a guard against `undef` values. The function can be used to call `fail`, or to
return a default value.

These two expressions are equivalent:

```puppet
if $x == undef { do_things() }
lest($x) || { do_things() }
```

**Example:** Using the `lest` function

```puppet
$data = {a => [ b, c ] }
notice $data.dig(a, b, c)
 .then |$x| { $x * 2 }
 .lest || { fail("no value for $data[a][b][c]" }
```

Would fail the operation because $data[a][b][c] results in `undef`
(there is no `b` key in `a`).

In contrast - this example:

```puppet
$data = {a => { b => { c => 10 } } }
notice $data.dig(a, b, c)
 .then |$x| { $x * 2 }
 .lest || { fail("no value for $data[a][b][c]" }
```

Would notice the value `20`

* Since 4.5.0
DOC
) do |args|
  Puppet::Parser::Functions::Error.is4x('lest')
end

Version data entries

116 entries across 116 versions & 3 rubygems

Version Path
puppet-retrospec-1.8.0 vendor/pup410/lib/puppet/parser/functions/lest.rb
puppet-retrospec-1.7.0 vendor/pup410/lib/puppet/parser/functions/lest.rb
puppet-5.3.7 lib/puppet/parser/functions/lest.rb
puppet-5.3.7-x86-mingw32 lib/puppet/parser/functions/lest.rb
puppet-5.3.7-x64-mingw32 lib/puppet/parser/functions/lest.rb
puppet-5.3.7-universal-darwin lib/puppet/parser/functions/lest.rb
puppet-4.10.12 lib/puppet/parser/functions/lest.rb
puppet-4.10.12-x86-mingw32 lib/puppet/parser/functions/lest.rb
puppet-4.10.12-x64-mingw32 lib/puppet/parser/functions/lest.rb
puppet-4.10.12-universal-darwin lib/puppet/parser/functions/lest.rb
bolt-0.20.3 vendored/puppet/lib/puppet/parser/functions/lest.rb
bolt-0.20.2 vendored/puppet/lib/puppet/parser/functions/lest.rb
bolt-0.19.1 vendored/puppet/lib/puppet/parser/functions/lest.rb
puppet-5.5.1 lib/puppet/parser/functions/lest.rb
puppet-5.5.1-x86-mingw32 lib/puppet/parser/functions/lest.rb
puppet-5.5.1-x64-mingw32 lib/puppet/parser/functions/lest.rb
puppet-5.5.1-universal-darwin lib/puppet/parser/functions/lest.rb
puppet-4.10.11 lib/puppet/parser/functions/lest.rb
puppet-4.10.11-x86-mingw32 lib/puppet/parser/functions/lest.rb
puppet-4.10.11-x64-mingw32 lib/puppet/parser/functions/lest.rb