简体   繁体   English

为什么 Ruby 解包有时会返回一个数组,但有时会返回值?

[英]Why does Ruby unpack sometimes return an array, but sometimes return values?

In the code below I use unpack() to ready some bytes from a string:在下面的代码中,我使用 unpack() 从字符串中准备一些字节:

  # unpack two bytes
  byte1, byte2 = "ABCDEFG".unpack("CC")
  # unpack one byte
  byte3 = "ABCDEFG".unpack("C")

When I hover over byte1 I see 65 , when I hover over byte2 I see 66 , but when I hover over byte3 I see [0] = 65 .当我将鼠标悬停在byte1我看到65 ,当我将鼠标悬停在byte2我看到66 ,但是当我将鼠标悬停在byte3我看到[0] = 65 Why is this?为什么是这样?

In the first example you're destructuring the returned array.在第一个示例中,您正在解构返回的数组。

In the second example you're not.在第二个例子中,你不是。

unpack always returns an array (as described in the docs) but what you do with that array will affect what's displayed in your editor/IDE. unpack总是返回一个数组(如文档中所述),但你与数组什么会影响到什么显示在您的编辑器/ IDE。 In the second example you're setting a single value to the return value of unpack , the array.在第二个示例中,您将单个值设置为unpack的返回值,即数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM