# How to properly iterate an array (using TDD)

**URL:** <https://www.ruby-forum.com/t/how-to-properly-iterate-an-array-using-tdd/110411>\
**Category:** Ruby\
**Created:** [July 26, 2007, 3:00am UTC](https://www.ruby-forum.com/t/how-to-properly-iterate-an-array-using-tdd/110411 "2007-07-26T03:00:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [July 26, 2007, 3:00am UTC](https://www.ruby-forum.com/t/how-to-properly-iterate-an-array-using-tdd/110411/1 "2007-07-26T03:00:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [July 26, 2007, 3:12am UTC](https://www.ruby-forum.com/t/how-to-properly-iterate-an-array-using-tdd/110411/2 "2007-07-26T03:12:39Z")

</div>

On 7/26/07, Al Cholic [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> expected = "5\n9"
> 
> ```
> 
> bom.each { |line|  
> 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.

The get\_component\_info method is returning the bom variable because that  
was  
the last thing evaluated.

bom.each{ stuff } will return bom if it’s the last statement.

I’m not sure where the “5\n9” comes from. I don’t see anything in the  
array  
or the get\_component\_info method that would give this value.

What are you trying to do?

Cheers  
Daniel

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [July 26, 2007, 3:14am UTC](https://www.ruby-forum.com/t/how-to-properly-iterate-an-array-using-tdd/110411/3 "2007-07-26T03:14:39Z")

</div>

> -----Original Message-----  
> I have setup a test that looks like this:
> 
> \<“5\n9”\> expected but was
> 
> ## PS this is my first attempt at TDD.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

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

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [July 26, 2007, 3:40am UTC](https://www.ruby-forum.com/t/how-to-properly-iterate-an-array-using-tdd/110411/4 "2007-07-26T03:40:41Z")

</div>

> result.join("\n")  
> end

Make that:

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