From: Wesley J Landaker Date: 2003-08-01T13:56:51+09:00 Subject: Re: A bundle of newbie queries On Thursday 31 July 2003 10:26 pm, Gawnsoft wrote: > 1) After the file has been opened and I've iterated through it once, > I can't successfully iterate through the opened file for something > else. e.g. > > dictionary2 = Hash.new(0) aFile.rewind (see http://www.rubycentral.com/book/ref_c_io.html#IO.rewind) > aFile.each_line { | eachLine | dictionary2[ /[0-9.]+/ ] = > dictionary2[ /[0-9.]+/ ] + 1 if eachLine.include?("anotherfile") } > 2) I then tried to get the no-longer usable file garbage-collected by > IRB-ing > > aFile = nil > > but even after evaluating this statement, another windows app still > fails to be able to over-write copyOfGeneral.txt > > What should I do to get Ruby to 'free up' the file? aFile.close If you don't close a file that you opened it will [probably(?)] get closed when it gets garbage collected. Note that you only need to close a file if you do this: f = File.open(...) do stuff with f f.close If you do it this way, it will get closed for you automatically: File.open(...) { |f| do stuff with f } > ----------- > 3) I tried to cut it down from 4 lines of code to 3 lines of code by > using: > (File.open("d:/Data/Logs/Savant/copyOfGeneral.txt")).each_line { > > |line| blah... } > > I'm sure I've seen snippets of code in the newsgroup that call > methods from the result of called methods, although the real reason I > tried it is because I'm currently learning Smalltalk where > > ( File open: "name of aFile" ) each_line: [ blah... ] # > pseudocode only this, in all probability, btw > > works as the parenthesised expression is evaluated first and returns > aFile, which is then sent the eac_line message This should work just fine; what kind of error are you getting? File.open(...).each_line { |line| # use line here } > What are the rules as to when object.method.method is a valid > Ruby construct? Is it ever a valid Ruby construct? It's practically always valid, unless you are doing something syntactically weird or ambiguous... so you can do stuff like this: line = "Apples are nice, but cheese whiz is better." line.chomp.gsub(/whiz/,'sauce').sub(",") { '' }.split.to_a.join("!!! ") #=> "Apples!!! are!!! nice!!! but!!! cheese!!! sauce!!! is!!! better." > ------------ > 4) I'd like to c'n'p a multi-line method into IRB all at once. Can > I? And if so, how? > > def countDownloads (aFile, aString) > > anIpNum = Regexp.new('[0-9.]+') > > aDictionary = Hash.new(0) > > aFile.each_line { | line | aDictionary[line.slice(anIpNum)] = > aDictionary[line.slice(anIpNum)] + 1 if line.include?(aString) } > > puts aDictionary.size > > end You should just be able to copy and paste it, it works fine here. =) > 5) I'd like to save this method as a .rb file, and invoke it from > IRB. How do I? If the code is in "file.rb", then in irb: irb> load "file.rb" > 6) I'd like to change the current Windows directory from within IRB. > How do I? > > Using irb_context, I saw there was an irb_path set (to be '(irb)' so > I tried: conf.irb_path="d:/data/grlcode" which returned: > NameError: undefined local variable or method `conf' for > # Dir.chdir("d:/data/grlcode") should work just fine. =) > > Cheers, > Euan Hope that helped! You'll probably get other [better?] answers from some other folks here on the list as well. -- Wesley J. Landaker - wjl@icecavern.net OpenPGP FP: 4135 2A3B 4726 ACC5 9094 0097 F0A9 8A4C 4CD6 E3D2