Ruby TypeError: no implicit conversion of String into Array

16,380

Change

size_list << size.slug.to_s

To

size_list << [size.slug.to_s]

In my case, the error occurred because the value I had put to array doesn't have [], so I wrap it with [] and it works.

Share:
16,380
gregf
Author by

gregf

Updated on June 04, 2022

Comments

  • gregf
    gregf almost 2 years

    I have a test that returns TypeError: no impliciit conversion of String into Array, when it hits a certain section of my code. If I run the code outside of rspec it runs just fine, so I'm not sure why this is happening.

    require 'spec_helper'
    require 'digital_ocean_size_list'
    
    describe Chef::Knife::DigitalOceanSizeList do
      subject { Chef::Knife::DigitalOceanSizeList.new }
    
      let(:access_token) { 'FAKE_ACCESS_TOKEN' }
    
      before :each do
        Chef::Knife::DigitalOceanSizeList.load_deps
        Chef::Config['knife']['digital_ocean_access_token'] = access_token
        allow(subject).to receive(:puts)
      end
    
      describe "#run" do
        it "should validate the Digital Ocean config keys exist" do
          expect(subject).to receive(:validate!)
          subject.run
        end
    ....
    

    It's testing the following code

    require 'chef/knife/digital_ocean_base'
    
    class Chef
      class Knife
        class DigitalOceanSizeList < Knife
          include Knife::DigitalOceanBase
    
          banner 'knife digital_ocean size list (options)'
    
          def run
            $stdout.sync = true
    
            validate!
    
            size_list = [
              ui.color('Slug',   :bold)
            ]
    
            client.sizes.all.each do |size|
              size_list << size.slug.to_s
            end
    
            puts ui.list(size_list, :uneven_columns_across, 1)
          end
        end
      end
    end
    

    The type error is coming from client.sizes.all.each. The code runs fine, I only get the type error when it's from rspec.