Search code examples
rubycsvprawn

Wrong prawn method or wrong object?


I am trying to get programm started where i cant read in a csv File an it prints the data out on a pdf-File. Now i have a problem. Heres is my Code:

------------------------------------
require_relative 'friends'

class List

  attr_accessor :list_name, :list

  def initialize(list_name)
    @list_name = list_name
    @list = []
  end

  def list_name
    @list_name
  end

  def liste
    @list
  end

  def wert(place)
    @list[place].to_s
  end

  def list_length
    @list.length
  end

  def print_list
    @list.each do |freunde|
    "#{freunde.name},#{freunde.age}"
    end
  end

  def add_friend(a_friend)
    @list.push(a_friend)
  end

  def load_friend(from_file)
      File.readlines(from_file).each do |line|
        add_friend(Freunde.from_csv(line))
      end
  end

end
-------------------------------------------

  require_relative 'list'

  class Friends

    attr_accessor :name,:age

    def initialize(name, age)
      @name = name
      @age = age
    end

    def self.from_csv(string)
      name, age = string.split(',')
      Freunde.new(name,age)
    end

   def friends
    @name
   end
  end   
-------------------------------------------

require 'prawn'
require_relative 'list'
require_relative 'friends'


class Generating
include Prawn::View

  def initialize
    @document = Prawn::Document.new(:page_size => "A4")
    @fontpath = File.expand_path("../data/fonts", __FILE__)
    liste1 = Listen.new("Friendslist")
    liste1.load_friend("test.csv")
    print_list
    save
  end

  def print_friends
    font("#{@fontpath}/Arial.ttf") do
        font_size 11
        text_box("#{liste1.print_list}", :at => [15,405], :height => 50, 
        :width => 250)
end
  end

  def save
    self.render_file "Hello.pdf"
  end
end
---------------------------------------------

When i now create a new generating-Object:

gen = Generating.new

then it fails the whole programm because the error says method unknow (print_list). Am i submitting the wrong object for the method(print_list), or am using the text output methods of prawn wrong?


Solution

  • print_list is an instance method of List class, and you call it on self object, which is there an instance of Generating. It should be:

    liste1 = Listen.new("Friendslist")
    liste1.load_friend("test.csv")
    #⇓⇓⇓⇓⇓⇓
    liste1.print_list