Joshua Drake wrote: > Hello, > > I am the author of the Programming in ruby on IBM Developworks. I have been > reviewing all the feedback I received on the first article. Most of the > feedback was positive but I did receive some negative feedback about > my use of Global variables, and the over simplified approach I used > for some of the examples. > > I am getting ready to release the second article, but I want to get > some feedback from the ruby community first. I would like the second > article to be more ruby centric and will need some help. > > I have a lot of experience in Perl and moderate experience with > Python. ruby I have only been using part time and we are increasing > our usage but I am having a hard time figuring out what is "good" > about ruby (excluding the pure OO). > > Anyway, without getting too wordy, I have included two example > scripts that I was going to include in the second article. I > would sincerely appreciate it if some of the more experienced > ruby programmers would provide some feedback on it. No, the > scripts are not terribly advanced. The first script opens a file > in append mode and asks for some input. The second allows you > to search through the file you created. > > I really want to present ruby in a good light with the next article. > Any help would be greatly appreciated. > > Sincereley, > > Joshau Drake > > == Begin Script > #!/usr/bin/ruby > begin > myfile = open('phonespec.txt', 'a') I'd use "File.open" instead of "open". I think the multitude of Ruby user do so as well. Do you know that File.open("phonespec.txt", "a") do |f| # .... do whatever with f end is equivalent to begin f = File.open(....) # .... do whatever with f ensure f.close end but much more elegnat. > $closing = "\n\nTo start the program again please type intro2.rb\n\tGood > Bye!\n\n" You don't have to use a global variable here! > > enterFirstName = "" #First Name in the addressbook > enterLastName = "" #Last Name in the Addressbook > enterPhoneNumber = "" #Phone Number in the addressbook > > print "Welcome to MyAddressbook. Please follow the prompts.\n" > print "If you wish to end data entry, you may do so at any time\n" > print "by type the word END into a prompt.\n\n" You could use print %{Welcome .... If you wish... by type.... } or print <<EOF .... EOF instead > while 1 better: while true or I'd use "loop" for an endless loop: loop do .... end > print "\n\tFirst Name: " > enterFirstName = STDIN.gets > enterFirstName.chop! The whole three inputs could be abbreviated, so I'd have written the example as: def input(closing, *prompts) prompts.collect { |pr| print "\n\t#{ pr }: " line = STDIN.gets.chop if line == "END" print closing return nil else line end } end CLOSING = "\n\nTo start the program again please type intro2.rb\n\tGood Bye!\n\n" print "Welcome to MyAddressbook. Please follow the prompts.\n" print "If you wish to end data entry, you may do so at any time\n" print "by type the word END into a prompt.\n\n" File.open("phonespec.txt", "a") do |myfile| loop { a = input(CLOSING, "First Name", "Last Name", "Phone Number") break if a.nil? # END was entered anywhere myfile << a.join("\t") << "\n" } end I think this is more Rubish way of doing your example. > if enterFirstName == "END" > print $closing > break > end > if enterFirstName != "END" > print "\n\tLast Name: " > enterLastName = STDIN.gets > enterLastName.chop! > end > if enterLastName == "END" > print $closing > break > end > if enterLastName != "END" > print "\n\tPhone Number: " > enterPhoneNumber = STDIN.gets > enterPhoneNumber.chop! > end > if enterPhoneNumber == "END" > print $closing > break > end > myfile.write(enterFirstName+"\t"+enterLastName+"\t"+enterPhoneNumber+"\n") > end > ensure > myfile.close > end > == end script 1 > == begin script 2 > phoneData = IO.readlines("phonespec.txt"); # Read in file into array, phoneData = File.readlines("phonespec.txt") > default separator of '\n' > > print "\n\nYou are searching for:\t" > searchData = STDIN.gets > searchData.chop! searchData = gets.chop > myreg = Regexp.new(searchData, "i") # The "i" option makes it > case-insensitive. > numres = 0 # Init number of results at 0. > > for data in phoneData # For each line of data in our array... > if myreg =~ data # Compare our regex against this line. > print "Found result: " + data # Output line that matched the regex. > numres += 1 # Increment number of results found. > else > end > end > > print "\n\n(Found " + String(numres) + " results, while searching for '" + use numres.to_s instead String(numres) and print a, b, c instead of print a + b + c > searchData + "' in 'phonespec.txt')\n\n" > == end script 2 > phoneData = IO.readlines("phonespec.txt"); # Read in file into array, phoneData = File.readlines("phonespec.txt") > default separator of '\n' > So I'd have written: print "\n\nYou are searching for:\t" searchData = gets.chop myreg = Regexp.new(searchData, "i") numres = 0 IO.foreach("phonespec.txt") do |data| if myreg =~ data print "Found result: ", data numres += 1 end end print "\n\n(Found #{ numres } results, while searching for '#{ searchData }' in 'phonespec.txt')\n\n" or more elegantly something like that: print "\n\nYou are searching for:\t" searchData = gets.chop myreg = Regexp.new(searchData, "i") found = File.readlines("phonespec.txt").grep(myreg) found.each {|f| print "Found result: #{ f }"} print "\n\n(Found #{ found.size } results, while searching for '#{ searchData }' in 'phonespec.txt')\n\n" Regards, Michael -- Michael Neumann merlin.zwo InfoDesign GmbH http://www.merlin-zwo.de