On 18/06/06, Graham Smith <[EMAIL PROTECTED]> wrote:
Hi,

I cannot find a way of initialising all items in a class as per this bit of
code.

#!/usr/bin/ruby

class File_list
        def initialize( row, name)
                @row = row
                @name = name
        end
        def name
                @name
        end
        def row
                @row
        end
        def ctime
                @ctime = File.stat(@name).mtime
        end
        def dur
                @dur = File.stat(@name).size/1700
        end
        def to_s
                "[EMAIL PROTECTED] [EMAIL PROTECTED] [EMAIL PROTECTED] [EMAIL 
PROTECTED]"

I think you need to say "[EMAIL PROTECTED] ... "

        end
end

z="/var/spool/voice/incoming/*.rmd"
files = Dir[z].sort_by { |f| [File.new(f).mtime, f] }
puts "File list sorted by time"
puts files
puts

a1 = File_list.new(1, files[0])
puts "Details of a1"
puts a1.to_s
puts "   Duration in Seconds = "+ a1.dur.to_s
puts "     Modification Time = "+ a1.ctime.to_s

puts a1.to_s

======================= EOF ==============
When I first call a1.to_s it only prints the row and name. Not until I call
a1.dur and a1.ctime individually will these two variables become initialised.

Is there any way to initialise these variables when I call File_list.new ?

Can't you put "@ctime = File.stat(@name).mtime" in initialize.
Similarly for '@dur' ?


Also is there a way to make this class into an array so as to hold a number of
rows?

I'd use aggregation.  Which is, you design your own class, and make
one of its members an array.  You then control access and how the
array is modified.

So you could take your Dir[...] and pass in the list of file names it
provides as a parameter to initialize.  May as well build an array of
File objects, rather than the mtime, unless there is a whopping
greating number of them.  I might also use a hash instead, and use the
file name as the key.

I think you could also use inheritance ie make your File_list a
specialised Array.
class File_list < Array
...
end

You have to use things like 'super' in your methods to invoke the
parent functionality (Array#push).  Not altogether sure about this
approach as I am more a newbie at this myself.



--
Regards,

Graham Smith
_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders

_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders

Reply via email to