为什么我要打印两次对象? [英] Why am I getting objects printed twice?

查看:47
本文介绍了为什么我要打印两次对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 irb 或 rails 中并使用 each 创建一些迭代时,我会在终端或浏览器中再次打印整个结构.示例:

When I am in irb or in rails and I create some iteration with each, I am getting the whole struct printed again in my terminal or inside the browser. Example:

a = [1,2,3,4]

a.each do |number|
  puts n
end

在irb终端或浏览器内的结果:

The result in irb terminal or inside the browser:

1
2
3
4

=> [1,2,3,4]

为什么这个=>[1,2,3,4] 出现在浏览器里面?我无法在我的页面中创建单个列表,因为整个结构都出现了.

Why does this => [1,2,3,4] appear inside the browser? I can't create a single list in my page because the whole structure appears.

推荐答案

Ruby 中的每个表达式都返回一个值;在irb中,你刚刚执行的表达式返回的值显示在=>之后.

Every expression in Ruby returns a value; in irb, the value returned by the expression you've just executed is displayed after =>.

Enumerable::each 的返回值是调用 each 的对象——在本例中,数组 [1,2,3,4]

The return value of Enumerable::each is the object that called each - in this case, the array [1,2,3,4]

这篇关于为什么我要打印两次对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆