我正在尝试启动程序,我无法在csv文件中读取它在pdf文件中输出数据。现在我有一个问题。 Heres是我的代码:
------------------------------------
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
---------------------------------------------
当我现在创建一个新的generate-Object:
gen = Generating.new
然后它失败了整个程序,因为错误说方法unknow(print_list)。我为方法(print_list)提交了错误的对象,还是使用了prawn的文本输出方法错了?
答案 0 :(得分:0)
print_list
是List
类的实例方法,您可以在self
对象上调用它,该对象是Generating
的实例。它应该是:
liste1 = Listen.new("Friendslist")
liste1.load_friend("test.csv")
#⇓⇓⇓⇓⇓⇓
liste1.print_list