From: Robert Klemme Date: 2010-11-04T17:11:05+09:00 Subject: Re: Regular Expression On Wed, Nov 3, 2010 at 5:45 PM, w_a_x_man wrote: > On Nov 3, 7:31 am, Robert Klemme wrote: >> I'd rather do: >> >> def check(input) >>  raise ArgumentError, "Illegal chars in sequence: %p" % input unless >> /\A[A-D]{0,4}\z/ =~ input >>  cnt = {'A' => 1, 'B' => 1, 'C' => 1, 'D' => 2} >>  input.scan /./ do |m| >>    raise ArgumentError, "Illegal sequence %p" % input if (cnt[m] -= 1) < 0 >>    # alt: return false >>  end >>  # alt: true >> end > > "We don't need no stinkin' loops!" > > def check input >  return false  unless /\A[A-D]{0,4}\z/ =~ input >  %w(A B C D).map{|s| input.count s}.zip( [1,1,1,2] ). >    map{|a,b| b-a}.all?{|n| n >= 0} > end The question is: what qualifies as a loop? Explicit loops are only done with "while", "until" and "for", Everything else is just a method call with a block. According to the strict loop definition my code does not have a loop either. According to the wide loop definition your code has a lot more loops than mine. I can spot at least five of them - plus a lot temporary Array instances. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/