Newbie block question

Discussion in 'Ruby' started by Dave Snowdon, Aug 20, 2006.

  1. Dave Snowdon

    Dave Snowdon Guest

    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
    Dave Snowdon, Aug 20, 2006
    #1
    1. Advertising

  2. Dave Snowdon wrote:
    > 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?


    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
    Robin Stocker, Aug 20, 2006
    #2
    1. Advertising

  3. Dave Snowdon

    Dave Snowdon Guest

    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
    Dave Snowdon, Aug 20, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Showjumper
    Replies:
    1
    Views:
    699
    Showjumper
    Mar 19, 2005
  2. Noozer

    Block DIV within a block DIV?

    Noozer, Jan 6, 2005, in forum: HTML
    Replies:
    3
    Views:
    11,361
    Mitja
    Jan 6, 2005
  3. Andy
    Replies:
    0
    Views:
    530
  4. morrell
    Replies:
    1
    Views:
    946
    roy axenov
    Oct 10, 2006
  5. Alvin
    Replies:
    8
    Views:
    968
Loading...

Share This Page