1. When I run a
particular .rb file, I would like a directory/folder to be created automatically
and in that my output result text file should be stored. In other words, a
folder should be automatically created at runtime which would hold a .txt file
containing the results. That folder should have the Date and Time (when the rb
file was run)as it folder name. Is there a way of doing it?? Please let me
know.
I was tempted not to answer this question, because I
thought you would learn so much more from answering it yourself. But
thinking about it turned out to be too much fun. These two methods (put
them into a module) work together to make a directory based on the current
minute.
class Time
# when called as a method on a Time object,
returns a string in the form
# YYYYMMDDhhmm
def timestring
self.year.to_s + self.month.to_s.rjust(2, "0") + self.day.to_s.rjust(2, "0") \
+ self.hour.to_s.rjust(2, "0") + self.min.to_s.rjust(2, "0")
end
end
def timestring
self.year.to_s + self.month.to_s.rjust(2, "0") + self.day.to_s.rjust(2, "0") \
+ self.hour.to_s.rjust(2, "0") + self.min.to_s.rjust(2, "0")
end
end
class Dir
def Dir.mkTimeStampDir
dirname = Time.now.timestring
def Dir.mkTimeStampDir
dirname = Time.now.timestring
begin
mkdir(dirname)
mkdir(dirname)
return
dirname
rescue
puts
"Unable to create a directory
named #{dirname}
return
nil
end
end
end
end
end
Now you can put
folder = Dir.mkTimeStampDir
in your code (checking for folder.nil?) and put a file
into the newly-created directory.
This illustrates, by the way, how we can add methods
to any of the existing Ruby classes, such that (if we add the code above to an
IRB session via the require statement), we can do stuff like
this:
irb(main):014:0> foo = Time.now
=> Sat Mar 04 16:37:29 Eastern Standard Time 2006
irb(main):015:0> foo.timestring
=> "200603041637"
=> Sat Mar 04 16:37:29 Eastern Standard Time 2006
irb(main):015:0> foo.timestring
=> "200603041637"
Gather enough stuff like this together into a single
module, and you can extend the language and IRB in whatever you like. I
LOVE Ruby.
2. I have grouped around
15 test cases in my test suite. When I run the test suite .rb file, everything
runs properly and at the last, the execution time is being displayed like
'Finished in ..... seconds.'.
But I would like to know the time taken for each of my test cases to
finish. Is there a way of doing it ? Can someone please let me know?
At the
beginning of whatever you wish to time, put a line
starttime = Time.now
At
then end of whatever you wish to time, put a line
endtime = Time.now
When
you want to determine the elapsed time,
elapsed = endtime - starttime
where
elapsed will be a value in seconds.
3. I would like to
increase the speed of execution. Please let me know how to do it.
I presume that you mean typing speed. Try callling
set_fast_speed
at the beginning of the slow bits.
---Michael B.
_______________________________________________ Wtr-general mailing list [email protected] http://rubyforge.org/mailman/listinfo/wtr-general
