Ruby 块似乎无法访问局部范围变量

发布于 2024-11-17 09:21:54 字数 1418 浏览 5 评论 0原文

我正在编写一个函数,该函数读取答案键,创建问题,然后将正确答案与该问题相关联。这是我的函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木落 2024-11-24 09:21:54

错误一定是在其他地方,这段代码应该可以工作。

编写一些单元测试并重构这个方法,它试图做太多事情。

此外,当您循环图像时,您可以摆脱 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 use each_with_index instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文