"@*(&SPAM&)*optonline.net" <""kboruff\"@*(&SPAM&)*optonline.net"> wrote in message news:<U4oDc.9527$OT6.7027622 / news4.srv.hcvlny.cv.net>... > Hey all, > > I've been spending the last week learning Ruby. Prior to that, I had > spent some time learning Python. For various reasons, it looks like I'm > gravitating more to Ruby. > > That being said, I decided to write a small program similiar to the UNIX > 'wc' program. > > Right now, it's very stripped down being that it only accepts input data > from STDIN if not a tty and I also haven't yet implemented the command > line arguments. > > For the context of this post, I only included the logic that gets/prints > the word count and gets/prints the length of the longest line since > these are the ones I have questions about. Here it is: > > > ----- Beginning of Program ----- > > #!/bin/env ruby > > # Read input from stdin only if not a tty. The only reason I gave such a > # constraint here was just to see that I could do it. It's one of the > # first things I do in learning a new language > if not STDIN.tty? > data = STDIN.read > end > > exit if not data > > > # PRINT THE WORD COUNT > > # I'm wondering if there's an easier way to do this. It would > # be nice of the String::count method accepted regex patterns > # and not just strings. > > # As it stands, this method creates a seperate array of words > # for which I get the count of. I would've rather done this > # without the extra overhead but I guess it's no big deal: it works! > printf("Word Count: %d\n", data.split(/\s/).length) > > > # GET THE LENGTH OF THE LONGEST LINE > > # If there's a more elegant solution than what I have below, I'm all > # ears > line_length = 0 > data.split(/\n/).each do |line| > line_length = line.length if line_length < line.length > end > > printf("Longest Line Length: %d\n", line_length) > > ----- End of Program ----- > > > My question here isn't correctness as much as elegance. I'm fairly sure > the solutions I've provided are correct (maybe); I'm just wondering if > anyone has a better solution. > > Thanks, > Keith P. Boruff For File.wc see "ptools", available on the RAA. Regards, Dan