You got that exception because the class String has no instance method each
:
String.instance_methods.include?(:each) #=> false
If packages
is a string need to operate on an array comprised of that string alone. We can use the method Kernel#Array to write:
Array(packages).each do |package|
Array(packages)
will return packages
if packages
is an array and will return [packages]
if packages
is a single element, here a string.
I think it’s better practice, however, to always pass an array to the method, even when the array contains a single element.
3
solved Ruby .each fails for single element