Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a reasonably full example that assumes the following</p> <ul> <li>Your list of products is contained in a file called veg.txt</li> <li>Your data is arranged across three lines per record with the fields on consecutive lines</li> </ul> <p>I am a bit of a noob to rails so there are undoubtedly better and more elegant ways to do this</p> <pre><code>#!/usr/bin/ruby class Vegetable @@max_name ||= 0 @@max_variety ||= 0 @@max_container ||= 0 attr_reader :name, :variety, :container def initialize(name, variety, container) @name = name @variety = variety @container = container @@max_name = set_max(@name.length, @@max_name) @@max_variety = set_max(@variety.length, @@max_variety) @@max_container = set_max(@container.length, @@max_container) end def set_max(current, max) current &gt; max ? current : max end def self.max_name @@max_name end def self.max_variety @@max_variety end def self.max_container() @@max_container end end products = [] File.open("veg.txt") do | file| while name = file.gets name = name.strip variety = file.gets.to_s.strip container = file.gets.to_s.strip veg = Vegetable.new(name, variety, container) products &lt;&lt; veg end end format="%#{Vegetable.max_name}s\t%#{Vegetable.max_variety}s\t%#{Vegetable.max_container}s\n" printf(format, "Name", "Variety", "Container") printf(format, "----", "-------", "---------") products.each do |p| printf(format, p.name, p.variety, p.container) end </code></pre> <p>The following sample file</p> <pre> Artichoke Green Globe, Imperial Star, Violetto 24" deep Beans, Lima Bush Baby, Bush Lima, Fordhook, Fordhook 242 12" wide x 8-10" deep Potatoes King Edward, Desiree, Jersey Royal 36" wide x 8-10" deep </pre> <p>Produced the following output</p> <pre> Name Variety Container ---- ------- --------- Artichoke Green Globe, Imperial Star, Violetto 24" deep Beans, Lima Bush Baby, Bush Lima, Fordhook, Fordhook 242 12" wide x 8-10" deep Potatoes King Edward, Desiree, Jersey Royal 36" wide x 8-10" deep </pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload