From: Frank Mitchell Date: 2001-10-20T14:46:47+09:00 Subject: [ruby-talk:22813] PLease condense my code First off, it was a pleasure to meet so many people on this list at RubyConf. As a complete Ruby amateur, I'm still getting used to Ruby's semantics and object library. (The syntax is a snap. Thanks, matz.) For my own education, then, I wonder if some old Ruby hand could help me condense some code I wrote way back in April. At the time I was trying to convince a group of Java programmers and QA guys that scripting languages beyond Bourne shell could make their lives easier. My example came from a small tool I wrote to automate progress reports. The file containing my 'todo' list contained what my manager wanted ... and a lot of stuff he didn't. So here's the Ruby code to strip out the latter: #!/usr/bin/env ruby do_print = true ($<).each_line { |line| case line when /=begin/ then do_print = false when /=end/ then do_print = true when /=[-a-zA-Z0-9. ]*=/ then # do nothing else if do_print then puts line end end } Unfortunately, for what it does, this seems awfully verbose, especially since this is the original (sorry) Perl version: #!/usr/bin/perl while (<>) { if (!(/=begin/../=end/) && !(/^=[-a-zA-Z0-9. ]*=/)) { print } } This afternoon, I tried to shrink down the Ruby, with no success; for example Ranges behave perfectly logically and therefore not like Perl's. To me, line and character count are far less important than legibility ... but I'd like to make a case for Ruby to our sysadmins who already use Perl, and from looking at router config files I suspect they value minimizing keystrokes over minimizing head-scratching. Thanks in advance to anyone who picks up the task. P.S. The "[-a-zA-Z0-9. ]" isn't something like "^=.*=\s*$" because those lines were subheadings to navigate my ever-growing todo list; I wanted to not match lines that had '='..'=' for some other reason. I was also in a hurry, and didn't have time to experiment with the second regex, or explore how it would deal with DOS linefeeds on a UNIX system. (The file has DOS linefeeds for attachment to Microsoft Exchange mail messages.) P.P.S. Feel free to tell me how horrible my Perl is, too. -- Frank Mitchell (frankm@bayarea.net) "What is luck? Luck is probability taken personally. Luck is the excitement of bad math." -- Penn Jillette