help with locked code and ilogic error ruby

0

Greetings. I have the following problem was finishing validating the following code and and it works well if I place a whole number works but it does not work well as this is the image I think it is a problem of the meter because if I omit it works but I need it for auto increase the row number that is reading from the file ...... if I put 5 instead of i it works since that is the line that was valid NADBY. I mean the problem is not that line is the data you receive ... additionally try by console line by line and verify and according to console everything is fine

class EncoderController < ApplicationController

  @encoderr = File.foreach('ojos.txt').with_object([]) do |line, result|
    if line.start_with?('NADBY')
      clave     = line[0..4]
      region    = line[6]
      proveedor = line[7...-2]
      cajas     = line[-2..-1]
      result << "#{clave} región #{region}, #{proveedor}, tipo de cajas #{cajas}"

    end
  end
end

     vista ------->>>>>  <h1> <%= @encoderr %> </h1>   <<------------vista
    
asked by Jonathan Zambrano 15.11.2017 в 21:03
source

1 answer

0

The problem is that line_three is null (ie nil ) when calling the method start_with? , make sure that line_three have a string assigned before executing that method.

Your code is very elaborate, so I'd better make a suggestion about the code you could use to achieve the same result:

@encoderr = File.foreach('ojo.txt').with_object([]) do |line, result|
  if line.start_with?('NADBY')
    clave     = line[0..4]
    region    = line[6]
    proveedor = line[7...-2]
    cajas     = line[-2..-1]
    result << "#{clave} región #{region}, #{proveedor}, tipo de cajas #{cajas}"
  end
end

This code will read each line ( line ) of the file ojo.txt , compare it against condition line.start_with?('NADBY') and, if so, add the line to the fix result . At the end, the value of the result fix will be assigned to the variable @encoderr .

To optimize a bit, I think it would be better to encapsulate the logic in a function, which you can call later; for example:

def parse_file(file)
  File.foreach(file).with_object([]) do |line, result|
    if line.start_with? ('NADBY')
      clave     = line[0..4]
      region    = line[6]
      proveedor = line[7...-2]
      cajas     = line[-2..-1]
      result << "#{clave} región #{region}, #{proveedor}, tipo de cajas #{cajas}"
    end
  end
end

puts parse_file('ojo.txt') # Imprime el resultado en pantalla

Assuming the content of the ojo.txt file is:

NADSU 78000mc0cl0Css
NADBY 7808810008659
PAT 1 21D 089
NADBY 6808810008657

the result would be then would be:

NADBY región 7, 80881000865, tipo de cajas 9
NADBY región 6, 80881000865, tipo de cajas 7

To correctly display each element of the @encoderr array in a view, you must iterate using each , for example:

<% @encoderr.each do |line| %>
  <h1><%= line %></h1>
<% end %>

Taking the previous example, this would generate the following HTML code in your view:

<h1>NADBY región 7, 80881000865, tipo de cajas 9</h1>
<h1>NADBY región 6, 80881000865, tipo de cajas 7</h1>
    
answered by 15.11.2017 / 22:09
source