2

Hi i am getting an error of

`+': can't convert String into Array (TypeError)

the data in the file is delimited with the TAB.

Data in the file is:

Hi! Welcome to
Hi! Welcome to  google
Hi! Welcome to  google  Technologies
Hi! Welcome to  google  Technologies    Hyderabad 
Hi! Welcome to  google  Technologies    Hyderabad   Telengana
Hi! Welcome to  google  Technologies    Hyderabad   Telengana   India 

read_file=File.open('C:/Users/x/1234567.txt', 'r+')
     read_file.each do |x| 
       #puts x.length   
       array_list=x.split(/\t/)
       #print array_list.length
       case array_list.length
         when 3,4
         puts "hi"
         when 5
         print array_list[0..3]
         when 6
         print array_list[0..3]
         print array_list[0..2] + array_list[4]
         when 7
         print array_list[0..3]
         print array_list[0..2] + array_list[4]
         print array_list[0..2] + array_list[5]
         when 8
         print array_list[0..3]
         print array_list[0..2] + array_list[4]
         print array_list[0..2] + array_list[5]
         print array_list[0..2] + array_list[6]
         else
           puts "Happy"
        end
    end
1
  • array_list[0..3].join(' ') Commented Feb 6, 2015 at 12:14

1 Answer 1

4

Array#+ concatenates two arrays:

array + other_array

but you're trying to concatenate a string:

array_list[0..2] + array_list[4]

This is because array_list[4] returns a single element.

You can use values_at to fetch multiple indices (or ranges of indices) at once:

array_list.values_at(0..2, 4)
Sign up to request clarification or add additional context in comments.

Comments

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.