0

我有一堂课,有一种方法:

class ScreenerProbe 
  def initialize
    @body = {}
  end

  def import_simple_matrix(questions, answers)    
    @body['aliases'] = answers.map { |pair| 
      return { val: pair["text"], text: pair["value"] }
    }
    
    # this code doesn't seem to run
    questions.each.with_index do |text, index|
      @body["questions"][index + 1] = {
        "title" => text,
        "type" => 'default',
      }
    end
end

问题是一些代码似乎没有运行,即使我希望它运行。

ruby 2.6.6p146(2020-03-31 修订版 67876)[x86_64-darwin20]

4

1 回答 1

3

从循环内部调用return时,它不仅会“退出”循环,还会从外部方法返回,在这种情况下import_simple_matrix

这可能不是您想要的,也是您期望的某些分配不会发生的原因。

于 2022-02-01T14:46:48.700 回答