Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

safe navigation operator #46

Closed
thewyzard44 opened this issue Apr 5, 2018 · 3 comments
Closed

safe navigation operator #46

thewyzard44 opened this issue Apr 5, 2018 · 3 comments

Comments

@thewyzard44
Copy link

thewyzard44 commented Apr 5, 2018

I would like to enable this nil checking syntax:

admins = node['maas']&.key?('admin_name') ? Array(node['maas']['admin_name']) : data_bag('metal_admins')

note the &. after the node['maas'] It essentially says if node['maas'] is nil, discontinue this expression and return nil.

This is the same as the elvis operator in other languages. (e.g. ?. in c#)

adding a '.rubocop.yml' to my project with:

AllCops:
  TargetRubyVersion: 2.4

effectively enables the use of &. however it introduces new cookstyle errors of recipes/default.rb:1:1: C: Missing magic comment # frozen_string_literal: true. in every other file in my cookbook. Now that chef 12 is EOL, all supported versions of chef are ruby 2.3+, of which, ruby 2.3 was the introduction of this operator.

I don't know how big of an issue that frozen_string_literal error is - if there were a PR to update the ruby version, could this error be suppressed at the same time without repurcussions?

otherwise, all of the code generators would need updated to include that 'frozen_string_literals' in all of their files in order to accept this update.

The same remains true if I use TargetRubyVersion: 2.3

@lamont-granquist
Copy link
Contributor

both of those changes were committed to master in #45

@coderanger
Copy link

Also the way to write that in Chef is node.read('maas', 'admin_name')

@lamont-granquist
Copy link
Contributor

lamont-granquist commented Apr 5, 2018

i should really advertise that more broadly because i kinda fixed that but nobody uses it... (and i forget about it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants