[英]Output value of puts in Ruby
I'm probably missing something really obvious here. 我可能在这里错过了一些非常明显的东西。 I have the following Ruby method:
我有以下Ruby方法:
def pair_array
return self.pair.each_slice(2) {
|x| puts x.join(" & ")
}.to_s
end
When I try to display the value of this method in my Rails view by calling @team.pair_array nothing appears, but the correct value gets printed on the console. 当我尝试通过调用@ team.pair_array在Rails视图中显示此方法的值时,什么都没有出现,但是正确的值会在控制台上打印出来。 I know this is probably because I'm using puts.
我知道这可能是因为我正在使用看跌期权。 How can I get the result of this method to display in my view?
如何获得此方法的结果显示在我的视图中?
You're confusing printing with returning a value. 您将打印与返回值混淆了。
puts
returns nil, and each_slice
does not return the result of the block anyway. puts
返回nil,并且each_slice
不返回该块的结果。 What you want is this: 您想要的是:
def pair_array
pair.each_slice(2).map {|arr| arr.join ' & '}
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.