From: Jim Menard Date: 2002-10-22T03:34:18+09:00 Subject: Re: functional Ruby equiv to this perl snippet Here's my attempt to squish that code down even further. Simon Cozens writes: > bobx@linuxmail.org (Bob) writes: > > # parses a text file looking for server names and ignoring lines > > # with start with the pound/hash sign. > > It's pretty bad Perl, so here it is translated into pretty bad Ruby... > > def load_server_list > filename = "serverlist.txt" > file = open(filename, "r") > servers = [] > while line = file.gets > line.chop! > next unless line =~ /\S/ > next if line =~ /^#/ > servers << line > end > file.close > return servers > end def load_server_list(file_name) servers = [] IO.readlines(file_name).each { | line | # Next line assumes input always has newlines line = line.chomp!.strip # chomp instead of chop next if line.empty? || line[0] == ?# # Use ?# just to be different servers << line } return servers end servers = load_server_list(ARGV[0] || "serverlist.txt") server_count = servers.length Jim -- Jim Menard, jimm@io.com, http://www.io.com/~jimm/ "I invented the term Object-Oriented, and I can tell you I did not have C++ in mind." -- Alan Kay