代码之家  ›  专栏  ›  技术社区  ›  Steven Aguilar

在使用next运算符后不移动到下一个对象的数组上迭代

  •  1
  • Steven Aguilar  · 技术社区  · 6 年前

       DICTIONARY = { 
    'TELEPHONE NUMBER area code 1'=> '66563943', #Primary Phone - Area code (000)
    'TELEPHONE NUMBER first 3 1'  => '66563943', #Primary Phone - (000) 000
    'TELEPHONE NUMBER Last 4 1'   => '66563943', #Primary Phone - (000) 000-0000
        }
    

    上面是我需要解析电话号码的字段。

      {
        "field": "66563943",
        "value": "(201) 785-9896"
      },
    

      elsif phone_number?(field_data[:field])
          parse_phone_number(field_data[:value])
          @phone_number_sections.each do |section|
             fill(field, section)
             field.next!
          end
        else
    

    这个 field_data

    [10] pry(#<PdfScrie>)> field_dataield_data
    => {:field=>"66563943", :value=>"(201) 785-9896"}
    

    但是,字段在迭代时不会移动。

    1] pry(#<PdfScrie>)> field
    => "TELEPHONE NUMBER area code 2"
    [2] pry(#<PdfScrie>)> section> section
    => "201"
    [3] pry(#<PdfScrie>)> field>)> field
    => "TELEPHONE NUMBER area code 2"
    [4] pry(#<PdfScrie>)> section> section
    => "201"
    [7] pry(#<PdfScrie>)> @phone_number_sections_sections
    => ["201", "785", "9896"]
    [8] pry(#<PdfScrie>)> field>)> field
    => "TELEPHONE NUMBER area code 2"
    [9] pry(#<PdfScrie>)> section> section
    => "201"
    [10] pry(#<PdfScrie>)> field_dataield_data
    => {:field=>"66563943", :value=>"(201) 785-9896"}
    

    @phone_number_sections 阵列。

     PHONE_NUMBER_FIELD_IDS = [
        TELEPHONE_NUMBER_1_id = "66563943"
      ].freeze
    

    我使用电话ID来验证字段是否需要解析:

     def parse_phone_number(phone_number)
        @phone_number_sections = phone_number.gsub(/\D+/, "").match(/(...)(...)(....)/).captures
      end
    

    enter image description here 这是用数据填充表单的方法:

     def fill_form_with_data(field, field_data)
        if address_field?(field_data[:field])
          break_address_into_state_city_zipcode(field_data[:value], field)
        elsif phone_number?(field_data[:field])
          parse_phone_number(field_data[:value])
          @phone_number_sections.each do |section|
             fill(field, section)
             field.next!
             binding.pry
          end
        else
          fill(field, field_data[:value])
        end
    

    为什么 section 不移动到数组中的下一个元素?

     def fill_out
        form_fields.each do |field|
          id = DICTIONARY[field]
          @user_submission_data
            .select {|fd| fd[:field] == id}
            .each {|field_data| fill_form_with_data(field, field_data) }
        end
      end
    

    field.next! 只有数组中的最后一个元素填充到PDF表单字段中,因为字段不会移动到 DICTIONARY 如下图所示。 enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   Arup Rakshit    6 年前

    写你的 fill_out

    def fill_out
      form_fields.each do |field|
        id = DICTIONARY[field]
        @user_submission_data
          .select {|fd| fd[:field] == id}
          .each do |field_data|
            if address_field?(field_data[:field])
              break_address_into_state_city_zipcode(field_data[:value], field)
            elsif phone_number?(field_data[:field])
              parse_phone_number(field_data[:value])
              fill(field, @phone_number_sections.shift)
            else
              fill(field, field_data[:value])
            end
          end
      end
    end
    

    不需要 fill_form_with_data 方法。当前代码中的问题是, @phone_number_sections 数组迭代到每个phone字段的最后一个值。所以字段得到 阵列。但是上面的代码会在IMO中修复它。