From: Robert Klemme Date: 2009-11-13T23:17:39+09:00 Subject: Re: DRYing a Regex 2009/11/13 Mark Thomas : > On Nov 13, 2:34 am, RichardOnRails > wrote: >> Hey Caleb & James, >> >> With your insights,  I was able to cut down 18 lines of somewhat >> obscure code to 6 lines that I find very readable.  That's such and >> improvement on the quality of the code. > > I believe you can go further. For example, these three lines: > >  sNewList = %{TMxxx CSCO COL INTC BRCM FDX AA CAT BUR FSLR MSFT', >               PNC HPQ CSCO AMAT ORCL FCX ABX PVTB XHB CSCO TM FDX} >  aRawNewList = sNewList.scan(/[A-Z]+/) >  aNewList = Set.new(aRawNewList ).to_a.sort > > can be replaced by one: > >  aNewList = %W{TMxxx CSCO COL INTC BRCM FDX AA CAT BUR FSLR MSFT', >                PNC HPQ CSCO AMAT ORCL FCX ABX PVTB XHB CSCO TM FDX} I don't think so because that appears to be input from the outside which is provided as single String. > (you can add .sort to the end but I don't think you need it) > > also, consider something like this: > >  hCurrentList.delete_if { |key,v| aNewList.include?key } Basically the question is which of the two is larger. But if you do it this way round (i.e. iterate the Hash and check for existence in the new list then that should definitively be a Set). Here's my suggestion require 'set' # dumy base current = {"CSCO" => 1, "COL" => 2, "INTC" => 3, "BRCM" => 4, "FOO" => 99} # user input input = %{TMxxx CSCO COL INTC BRCM FDX AA CAT BUR FSLR MSFT PNC HPQ CSCO AMAT ORCL FCX ABX PVTB XHB CSCO TM FDX} # algorithm symbols = input.scan(/[A-Z]+/) deduped = symbols.to_set old_size = current.size deduped.each {|sym| current.delete sym} p(deduped.sort, 10, sprintf("New List (unique:%d, dups:%d, deleted:%s)", deduped.size, symbols.size - deduped.size, old_size - current.size), true) p current Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/