0

I am having some trouble with ruby syntax in chef (the configuration management tool).

The difference I am dealing with is assigning attributes such as below:

runner = ChefSpec::Runner.new(platform: "ubuntu", version: version)
runner = ChefSpec::Runner.new(:platform => "ubuntu", :version => version)

I need to be able to switch between these two syntax's as they seem to work in different versions of chef/ruby - we will be upgrading but for now need a fix.

I'm novice with regexp, but have been trying like this in python:

for line in fileinput.input(inplace=1, backup='.bak'):
  line = re.sub('(\w\w...): ',':"\1" =>', line.rstrip())
  print(line)

I don't know what the attribute name will be, or what precedes it, only that a single : follows a word (of 2 or more characterss). I'm happy assuming that there will always be a space after the : but I seem to be having trouble "picking up" the attribute name to replace and re-use later.

1 Answer 1

1

the following should work:

for line in fileinput.input(inplace=1, backup='.bak'):
  line = re.sub('(\w+): ', ':\g<1> => ', line.rstrip())
  print(line)

see regex demo, or repl demo

Sign up to request clarification or add additional context in comments.

1 Comment

Seems to be spot on! Thanks for the quick reply. I still need to work out exactly why it's that syntax, but all part of learning. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.