# Newbie block question

**URL:** <https://www.ruby-forum.com/t/newbie-block-question/65676>\
**Category:** Ruby\
**Created:** [August 20, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/newbie-block-question/65676 "2006-08-20T14:00:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Snowdon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed655f/32.png) [@Dave\_Snowdon](https://www.ruby-forum.com/u/Dave_Snowdon)\
**Post date:** [August 20, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/newbie-block-question/65676/1 "2006-08-20T14:00:38Z")

</div>

Hi folks

I’m working on some code that extracts data from a set of XML files  
using REXML stores this as arrays of arrays of floats (one array of  
floats per file), does some processing and then writes the results to 4  
text files for visualisation with gnuplot.

However, what happens is that although 4 files get created all the data  
gets saved in the first one to be opened. It’s as if the block in  
writeMatrix() stores the reference to the first File object and uses  
that on the other 3 method invocations. If that’s the case then I’ve  
obviously failed to understand the appropriate way to use a block.

What would be a better way to code this in ruby?

thanks

Dave

Here’s a portion of the class handling writing the files.

class TuneInputData

… snip snip …

def writeData()

```
writeMatrix(@@LUMINANCE_DATA_FILE, @imageVector)

writeMatrix(@@VOICE_DATA_FILE, @voiceVector)

writeMatrix(@@SOUND_DATA_FILE, @soundVector)

writeMatrix(@@RHYTHM_DATA_FILE, @rhythmVector)

writeColumn(@@COLOURFULNESS_DATA_FILE, @imageColourfulness)

```

end

def writeMatrix(filename, matrix)

```
file = File.new(filename, "w")

matrix.each { |row| writeRow(file, row) }

```

end

def writeRow(file, row)

```
outString = ""

row.each { |v| outString = outString + v.to_s + " " }

file.puts(outString)

```

end

… snip snip …

end

---

<div class="post-metadata">

**Author:** ![Dave\_Snowdon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed655f/32.png) [@Dave\_Snowdon](https://www.ruby-forum.com/u/Dave_Snowdon)\
**Post date:** [August 20, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/newbie-block-question/65676/2 "2006-08-20T14:30:40Z")

</div>

Dave Snowdon wrote:

> that on the other 3 method invocations. If that’s the case then I’ve  
> obviously failed to understand the appropriate way to use a block.
> 
> What would be a better way to code this in ruby?

Hi,

So I took your code, made it look more like Ruby code (snake\_case),  
simplified it, added example data and ran it. The resulting files were  
all as I think they should be. The problem you are having has to lie  
somewhere else in your code. Are you shure the class variables  
\*\_DATA\_FILE are right, or maybe they all contain the same file name?

Anyway, here’s the code:

class TuneInputData

@@LUMINANCE\_DATA\_FILE = “luminance.data”  
@@VOICE\_DATA\_FILE = “voice.data”  
@@SOUND\_DATA\_FILE = “sound.data”  
@@RHYTHM\_DATA\_FILE = “rhythm.data”  
@@COLOURFULNESS\_DATA\_FILE = “colourfulness.data”

def initialize  
@image\_vector = [[1, 2, 3], [3, 2, 1]]  
@voice\_vector = [[4, 5, 6], [6, 5, 4]]  
@sound\_vector = [[7, 8, 9], [9, 8, 7]]  
@rhythm\_vector = [[10, 11, 12], [12, 11, 10]]  
end

def write\_data  
write\_matrix(@@LUMINANCE\_DATA\_FILE, @image\_vector)  
write\_matrix(@@VOICE\_DATA\_FILE, @voice\_vector)  
write\_matrix(@@SOUND\_DATA\_FILE, @sound\_vector)  
write\_matrix(@@RHYTHM\_DATA\_FILE, @rhythm\_vector)  
# write\_column(@@COLOURFULNESS\_DATA\_FILE, @image\_colourfulness)  
end

def write\_matrix(filename, matrix)  
File.open(filename, ‘w’) do |file|  
file.puts matrix.collect{ |row| row.join(’ ') }  
end  
end

end

t = TuneInputData.new  
t.write\_data

Hope it helped,  
Robin

---

<div class="post-metadata">

**Author:** ![Dave\_Snowdon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed655f/32.png) [@Dave\_Snowdon](https://www.ruby-forum.com/u/Dave_Snowdon)\
**Post date:** [August 20, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/newbie-block-question/65676/3 "2006-08-20T18:02:58Z")

</div>

Hi Robin

Many thanks for the help - much appreciated. You were right, the problem  
was elsewhere - I finally tracked it down to the code reading the data.

Your code to output the data was far more elegant than mine and I’ve  
updated my class to match.

I can see I’ll have to kick the camelCase habit too - I guess I’ve spent  
too long writing in Java.

thanks

Dave
