RSPEC Practice 3 Array Extensions #Two Sum

I’m confused as to why the 0 element is being counted twice in my code below. I think that the cut_arr variable should eliminate that problem. Where is it going wrong?

class Array
def two_sum
all_pairs = []
self.each_index do |idx1|
cut_arr = self[0…idx1] + self[idx1 + 1…-1]
cut_arr.each_index do |idx2|
if self[idx2] + self[idx1] == 0
all_pairs << [idx1, idx2].sort
end
end
end
all_pairs.sort.uniq
end
end

Hi Gabe,

Your range self[0...idx1] includes the 0th index every time. This is an excellent time to practice using the debugger to step through your code and take a look at how your variables change line by line!

If you haven’t used byebug yet, you’ll want to require "byebug" at the top of your page. Then type byebug on it’s own line inside your method definition. When you run the specs again, your code will pause at the byebug marker and you can step through it line by line by tying s You’ll want to type display self and display cut_arr so that they auto update every time you step through your code. each time.

Another thing (perhaps more relevant) to take a look at is the line cut_arr.each_index. Is this doing what you expect? Or do you want to iterate through the integers you’ve saved at each index using each?

I tried using the debugger. I haven’t used it much since the lesson on it a few modules back. I entered pry and did load ‘00_array_extensions.rb’ and I get the following message: “cannot load such file – 00_array_extensions.rb”

The easiest way to use the debugger is by running the specs, rather than using pry. When you run the spec file normally, it will pause your code when it reads the debugger in your definition.