"Ralph Mason" <ralph.mason / telogis.com> writes:

This would be even more elegant using scan instead of =~, $' and $`
(I think of $' and $` as rather perlish)

> require "net/http"
> 
> if  ARGV.size == 0 
>  puts  "Usage: dictionary term"
>  exit
> end
> 
> a = Net::HTTP.get("www.dictionary.com","/cgi-bin/dict.pl?term=#{ARGV[0]}")
> 
> if ( a=~/No entry found for/ ) 
>  puts "No entry found for '#{ARGV[0]}' - Suggestions:\n\n"
>  
>  while $' =~/<a href="\/cgi-bin\/dict.pl\?term=\w+">(\w+)/
>   puts $1
>  end

Instead of the while loop:
  a.scan( /<a href="\/cgi-bin\/dict.pl\?term=\w+">(\w+)/ ) { |res|
    puts res
  }


> else
>  while a =~ /<!-- resultItemStart -->/
>   $' =~ /<!-- resultItemEnd -->/
>   a=$'
> 
>   puts  $`.gsub(/<.*?>/m){ |i| 
>    case i
>     when "<b>"
>      ""
>     when "</b>"
>      "\n"
>     when "<p>"
>      ""
>     else
>      ""
>    end
>    }
>  end

Instead of the while loop:
  a.scan( /<!-- resultItemStart -->(.*?)<!-- resultItemEnd -->/m ) { |res|
    puts res.to_s.gsub( /<.*?>/m ) { |i|
      i =~ /<\/b>/ ? "\n" : "" #/ just to please my emacs highlighting
    }
  }

> end

-- 
mvh
Eirik Meland