0

I'm trying to make an area that outputs the following: [@month, @monthly_count] so that the complete output looks like this: [["January", 0], ["February", 0], ["March", 0], ["April", 2], ["May", 3], ["June", 19], ["July", 0], ["August", 0], ["September", 0], ["October", 0], ["November", 0], ["December", 0]]

I'm currently getting the error that TypeError - no implicit conversion of String into Integer: on the line @monthly_value_created[i] << @months[i] below.

Here is my code:

@months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
@monthly_idea_value = [[0], [0], [0], [3], [35], [744], [0], [0], [0], [0], [0], [0]] 
#Array of arrays of integers isn't best, but unless it's causing the error I'm not worried about it right now.
#Bc the .flatten! method at the end fixes the output

      @monthly_value_created = Array.new(12){Array.new}
      i = 0
      12.times do |i|
        @monthly_value_created[i] << @months[i]
        @monthly_value_created[i] << @monthly_idea_value[i]
        @monthly_value_created.flatten!
        i += 1
      end

How can I avoid this error?

3 Answers 3

1

You can use Array#zip

@months.zip(@monthly_idea_value).map &:flatten

You don't need to map and flatten, if @monthly_idea_value is an Array of integers, instead of an Array of Arrays.

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

2 Comments

Would this replace the entire iteration? from i=0 to i +=1?
Yes it does. And there are many mistakes in your code, which 2called-chaos has explained in detail. Read that, and also learn how loops work in Ruby.
0

Might probably be more efficient to do:

@months.map.each_with_index do |month, index|
  [month, @monthly_idea_value[index][0]]
end

Comments

0

So you had two mistakes in your code.

First being incrementing i yourself while #times already passes the variable.

The second mistake you made is that you flattened the wrong thing (you only want to flatten the sub-array not the array structure you build.

@months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
@monthly_idea_value = [[0], [0], [0], [3], [35], [744], [0], [0], [0], [0], [0], [0]]

@monthly_value_created = Array.new(12){Array.new}
12.times do |i|
  @monthly_value_created[i] << @months[i]
  @monthly_value_created[i] << @monthly_idea_value[i]
  @monthly_value_created[i].flatten!
end

=> [["January", 0], ["February", 0], ["March", 0], ["April", 3], ["May", 35], ["June", 744], ["July", 0], ["August", 0], ["September", 0], ["October", 0], ["November", 0], []]

Still fylooi's or Santhosh's approach is better and more ruby-ish :)

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.