How to properly iterate an array (using TDD)

A

Al Cholic

Hello,

Im trying to iterate an array, but its not working.

I have setup a test that looks like this:

def test_getting_component_info
original = ["1 1 C1 0805,C56p 0805", "2 5 C2,C5,C19,C24,C25
0603,C0.01 0603 50V +/-10% ECU-V1H103KBV Panasonic 2011032601"]
expected = "5\n9"

assert_equal(expected, get_component_info(original))
end

The string elements are seperated by tabs.


And here is my method that I am testing:

def get_component_info(bom)
bom.each { |line|
line.split("\t").length
}
end

And here is what the test script says:
<"5\n9"> expected but was
<["1\t1\tC1\t0805,C56p\t0805",
"2\t5\tC2,C5,C19,C24,C25\t0603,C0.01\t0603\t50V
+/-10%\tECU-V1H103KBV\tPanasonic\t2011032601"]>.

The mehtod seems to be returning the same array that im passing in.
What am I doing wrong? Without the array iteration Im able to split it
by tabs and do everything but as soon as I put the iteration block,
everything messes up.

Can someone please help.

PS this is my first attempt at TDD.
 
M

Michael Fellinger

Array.each returns self. Your function will have to store the results of the
split lengths somehow.

Such as:

def get_component_info(bom)
result = Array.new
bom.each { |line|
result << line.split("\t").length
}
result.join("\n")
end

Make that:

def get_component_info(bom)
bom.map{|l| l.split("\t").size }.join("\n")
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top