[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to properly iterate an array (using TDD

Kristen

7/26/2007 1:01:00 AM

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.
--
Posted via http://www.ruby-....

1 Answer

Michael Fellinger

7/26/2007 1:40:00 AM

0

> 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