From: Gawnsoft Date: 2003-08-01T13:26:37+09:00 Subject: A bundle of newbie queries I've finally overcome my newbie embarrassment enough to post about actual code -------------------- After a couple of years(!) of about-to, I finally leapt over enough conceptual hurdles to use Ruby for something. (RubyWin 0.0.3.5, with Ruby 1.6.4) (And thank you Dave and Andy for providing the book with the distro) I was really quite insanely pleased that my first use of Ruby did something really quite involved - it counts the number of unique IP addresses of people who have downloaded a particular file from my web-server - and did it in four lines of code! aFile = File.open('D:/Data/Logs/Savant/copyOfGeneral.txt') aDictionary = Hash.new aFile.each_line { | line | aDictionary[line.slice(/[0-9.]+/)] = 1 if line.include?("plastic_1.1_lite-UMLtool-fw.exe") } puts aDictionary.size And then I amended it to tell me how often the file was downloaded from each unique IP address by changing the following lines to: aDictionary = Hash.new(0) aFile.each_line { | eachLine | aDictionary[ /[0-9.]+/ ] = aDictionary[ /[0-9.]+/ ] + 1 if eachLine.include?("plastic_1.1_lite-UMLtool-fw.exe") } All well and good - and I was surprised how quickly I got my head round file handling, hashes and regular expressions, and delighted at the way I could use a regexp as the index of a hash. (Btw, the file in question is a quite competent little UML diagramming tool - freeware, for Windows, less than 2MB, which autogenerates Java stub code. If anyone is interested in that sort of thing, it's available from the Smalltalk links page in my sig) I have some fresh and queries and problems though - 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.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? ----------- 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 What are the rules as to when object.method.method is a valid Ruby construct? Is it ever a valid Ruby construct? ------------ 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 ------------ 5) I'd like to save this method as a .rb file, and invoke it from IRB. How do I? ------------ 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 # Cheers, Euan Gawnsoft: http://www.gawnsoft.co.sr Symbian/Epoc wiki: http://html.dnsalias.net:1122 Smalltalk links (harvested from comp.lang.smalltalk) http://html.dnsalias.net/gawnsoft/smalltalk