Problem with sub() method in role search after Chef 11->12


Hello forum,

I have a recipe which was running fine under Chef 11 (yes, sorry, I know this is old...):

allroles = search(:role, "name:*", "X_CHEF_id_CHEF_X asc")
allroles.each do |key|
  rolename = key
  rolename = rolename.sub(/role.(.*)./, '\1')

Now after we had to move from v11 to v12, this breaks:
ERROR: undefined method `sub' for #Chef::Role:0x00000014f6d538

It seems the search delivered role objects as expected....can someone direct me in the right direction on why this does not work anymore?

thanks in advance!
S. Koenig


Try .gsub instead of sub, I think sub got removed.


Thanks for the hint. However the result is strangely enough the same:

undefined method `gsub'

edit: if I .to_s the rolename above, it seems to work.....

rolename = key.to_s


Your code inside the loop is using the entire role object. I'm not sure why you were able to treat it as a string in the past, but as you've found you can convert to a string with .to_s. If you just want to get the name out, though, it would be much easier to use the .name method, e.g.,

allroles.each do |role|
  rolename =
  # etc