From: Bill Kelly Date: 2006-03-11T13:41:00+09:00 Subject: Re: Help me understand why the Ruby block is slower than wit Hi, From: "Mark Devlin" > > Solely for my own amusement, since I'm still trying teach myself Ruby... > > File.open("./words").read.split.collect! {|x| x if x.length == 10 && > x.split(//).uniq! == nil}.compact!.each {|x| puts x } One detail here is the file handle is not being closed. A few alternatives that close the file: # open with block File.open("./words"){|f| f.read.split.collect! {|x| x if x.length == 10 && x.split(//).uniq! == nil}.compact.each {|x| puts x } } # File.read method File.read("./words").split.collect! {|x| x if x.length == 10 && x.split(//).uniq! == nil}.compact.each {|x| puts x } # IO.readlines method IO.readlines("./words").collect! {|x| x if x.length == 11 && x.split(//).uniq! == nil}.compact.each {|x| puts x } Note, used length 11 because readlines keeps linefeeds; also changed all to non-bang form of compact, as compact! would return nil if it didn't do any work. (I.e. if all words in the input satisfied the criteria, collect! would have returned nil, and we'd have gotten a NoMethodError: undefined method `each' for nil:NilClass.) Regards, Bill