【问题标题】:Making a for X in Y loop return whatever is returned by the statements it contains使 for X in Y 循环返回它包含的语句返回的任何内容
【发布时间】:2010-11-29 03:30:31
【问题描述】:

以下语句...

content_tag(:li, concept.title)

...返回类似:

<li>"My big idea"</li>

以下方法定义在调用时返回相同:

def list_of_concepts(part)
 content_tag(:li, concept.title)
end 

原来如此……

def list_of_concepts(part)
 content_tag(:li, part.concepts.first.title)
end  

但是下面...

def list_of_concepts(part)
  for concept in part.concepts
    content_tag(:li, concept.title)
  end
end  

...在我看来只是给了我一堆英镑符号(“#”),就像它返回真假或计数,而不是content_tag 返回的任何东西。如何让它返回 content_tag 返回的内容?

再次感谢,

史蒂文。

【问题讨论】:

    标签: ruby loops return for-loop


    【解决方案1】:

    for 循环不返回您的数据,试试这个:

    def list_of_concepts(part)
      part.concepts.map { |c| content_tag(:li, c.title) }.join
    end
    

    【讨论】:

      猜你喜欢
      • 2020-09-25
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多