Method: Hash#subset

Defined in:
lib/rwd/ruby.rb

#subset(fields, values, results = nil, exact = true, emptyline = nil, joinwith = nil) ⇒ Object



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/rwd/ruby.rb', line 598

def subset(fields, values, results=nil, exact=true, emptyline=nil, joinwith=nil)
  fields	= [fields]		unless fields.kind_of? Array
  values	= [values]		unless values.kind_of? Array
  results	= [results]		unless results.kind_of? Array
  emptyline	= emptyline.downcase	unless emptyline.nil?
  res		= self.dup
  res.delete_if {true}

  self.each do |k, l|
    ok	= true

    case l.class.to_s
    when "String"
      c		= l.splitwords
      correction	= 1
      joinwith	= " "	if joinwith.nil?
    when "Array"
      c		= l
      correction	= 0
    end

    #catch :stop do
      values2	= values.dup
      fields.each do |f|
        v	= values2.shift
        v	= v.downcase	unless v.nil?
        if emptyline.nil? or (not v == emptyline)
          if exact
            unless (v.nil? or c[f-correction].downcase == v)
              ok	= false
              #throw :stop
            end
          else
            unless (v.nil? or c[f-correction].downcase.include?(v))
              ok	= false
              #throw :stop
            end
          end
        end
      end
    #end

    if ok
      res2	= []
      if results == [nil]
        res2	= c
      else
        results.each do |n|
          res2 << c[n-correction]
        end
      end
      res2	= res2.join(joinwith)	unless joinwith.nil?
      res[k]	= res2
    end
  end

  return res
end