Created
January 21, 2019 22:11
-
-
Save parzibyte/e3af6353d547d5dba5d61ecbcaaa68a1 to your computer and use it in GitHub Desktop.
Leer archivo CSV created by parzibyte - https://repl.it/@parzibyte/Leer-archivo-CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Leer contenido CSV de un archivo con Ruby | |
@author parzibyte | |
=end | |
require 'csv' #La clase que nos ayuda | |
NOMBRE_ARCHIVO = "productos.csv" | |
# Una simple clase para encapsular todo | |
class Producto | |
attr_reader :descripcion, :precio_compra, :precio_venta, :cantidad | |
def initialize(descripcion, precio_compra, precio_venta, cantidad) | |
@descripcion = descripcion | |
@precio_compra = precio_compra | |
@precio_venta = precio_venta | |
@cantidad = cantidad | |
end | |
# Método to string | |
def to_s | |
"Descripción #{descripcion}, P.C. #{precio_compra}, P.V. #{precio_venta}, cantidad #{cantidad}" | |
end | |
end | |
# Un arreglo de productos en donde vamos a poner los datos | |
productos = [] | |
CSV.foreach(NOMBRE_ARCHIVO) do |fila| # Iterar línea o fila por fila | |
productos.push Producto.new(fila[0], fila[1], fila[2], fila[3]) | |
end | |
# Imprimir | |
puts productos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Galletas | 10 | 15 | 100 | |
---|---|---|---|---|
Papas fritas | 15 | 17 | 10 | |
Chocolate | 3 | 5 | 50 | |
Jugo de manzana | 9 | 11 | 200 | |
Galletas | 10 | 15 | 100 | |
Papas fritas | 15 | 17 | 10 | |
Chocolate | 3 | 5 | 50 | |
Jugo de manzana | 9 | 11 | 200 | |
Galletas | 10 | 15 | 100 | |
Papas fritas | 15 | 17 | 10 | |
Chocolate | 3 | 5 | 50 | |
Jugo de manzana | 9 | 11 | 200 | |
Galletas | 10 | 15 | 100 | |
Papas fritas | 15 | 17 | 10 | |
Chocolate | 3 | 5 | 50 | |
Jugo de manzana | 9 | 11 | 200 | |
Galletas | 10 | 15 | 100 | |
Papas fritas | 15 | 17 | 10 | |
Chocolate | 3 | 5 | 50 | |
Jugo de manzana | 9 | 11 | 200 | |
Galletas | 10 | 15 | 100 | |
Papas fritas | 15 | 17 | 10 | |
Chocolate | 3 | 5 | 50 | |
Jugo de manzana | 9 | 11 | 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment