Ruby 块似乎无法访问局部范围变量
我正在编写一个函数,该函数读取答案键,创建问题,然后将正确答案与该问题相关联。这是我的函数:
def self.save_images_in_dir(dir)
answer_key_file = Dir.glob(dir+"/*.{rtf,txt}").first
answer_key = Array.new
if answer_key_file
puts "found key"
File.open(answer_key_file, "r") do |infile|
while (line = infile.gets)
if line.match(/^\d+[.]\s+/)
num = line.match(/\d+/)
answer = line.gsub(/\d+[.]\s+/,"") # Take out the 1.
answer.chomp!
answer_key.push(answer.to_s)#answer_key[num.to_s]=answer.to_s
puts "number #{num} is #{answer.to_s}"
end
end
end
end
images = Dir.glob("#{dir}*.{png,jpeg,jpg,gif}").sort_by {|file| File.ctime(file) }
counter = 0
answer_key.each do |q|
puts "before entering: #{q}"
end
images.each do |img|
q = self.new
q.tags = get_tags(img)
q.correct_answer = answer_key[counter]
puts "---------Answer is:#{answer_key[counter]}--------\n"
q.photo = File.open(img)
if q.correct_answer.nil?
puts "answer is nil"
end
counter = counter + 1
end
end
这是进入 images.each 块之前的输出片段。
输入之前:D
输入之前:A
输入之前:A
输入之前:C
输入之前:A
找到密钥
---------答案是:--------
答案是 nil
有谁知道为什么answer_key会“重置”,而且,为什么在图像块中评估时answer_key.count会返回0?我知道块应该从调用它们的地方继承本地范围...为什么answer_key 不会被传递?
I'm writing a function that reads in an answer key, creates a question, and then associates the right answer with that questions. Here's my function:
def self.save_images_in_dir(dir)
answer_key_file = Dir.glob(dir+"/*.{rtf,txt}").first
answer_key = Array.new
if answer_key_file
puts "found key"
File.open(answer_key_file, "r") do |infile|
while (line = infile.gets)
if line.match(/^\d+[.]\s+/)
num = line.match(/\d+/)
answer = line.gsub(/\d+[.]\s+/,"") # Take out the 1.
answer.chomp!
answer_key.push(answer.to_s)#answer_key[num.to_s]=answer.to_s
puts "number #{num} is #{answer.to_s}"
end
end
end
end
images = Dir.glob("#{dir}*.{png,jpeg,jpg,gif}").sort_by {|file| File.ctime(file) }
counter = 0
answer_key.each do |q|
puts "before entering: #{q}"
end
images.each do |img|
q = self.new
q.tags = get_tags(img)
q.correct_answer = answer_key[counter]
puts "---------Answer is:#{answer_key[counter]}--------\n"
q.photo = File.open(img)
if q.correct_answer.nil?
puts "answer is nil"
end
counter = counter + 1
end
end
and here's a snippet of the output right before it enters the images.each block.
before entering: D
before entering: A
before entering: A
before entering: C
before entering: A
found key
---------Answer is:--------
answer is nil
Does anyone know why answer_key would "reset", and, furthermore, why answer_key.count would return 0 when evaluated within the images block? I understand that blocks should inherit the local scope from where they are called...any reason why answer_key would not be passed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误一定是在其他地方,这段代码应该可以工作。
编写一些单元测试并重构这个方法,它试图做太多事情。
此外,当您循环图像时,您可以摆脱
counter
并使用each_with_index
代替。The mistake must be somewhere else, this code should work.
Write a few unit tests and refactor this method, it's trying to do too many things.
Also, when you loop over the images, you can get rid of
counter
and useeach_with_index
instead.