📜  红宝石 |数组 pack()函数

📅  最后修改于: 2022-05-13 01:55:32.550000             🧑  作者: Mango

红宝石 |数组 pack()函数

Array#pack() : pack()是一个 Array 类方法,它根据 aTemplateString 中的指令将 arr 的内容返回为二进制序列

示例 #1:

# Ruby code for pack() method
  
# declaring array
a = [18, 22, 33, nil, 5, 6]
  
# declaring array
b = [1, 4, 1, 1, 88, 9]
  
# declaring array
c = [2, 3, 1, 8]
  
# pack method example
puts "pack() method form : #{a.pack("ccc")}\n\n"
  
puts "pack() method form : #{b.pack("efe")}\n\n"

输出 :

pack() method form : !

pack() method form : 

示例 #2:

# Ruby code for pack() method
  
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
b = ["cow", nil, "dog"]
  
# declaring array
c = ["maths"]
  
# pack method example
puts "pack() method form : #{a.pack("A3A3A3")}\n\n"
  
puts "pack() method form : #{b.pack("a3a3a3")}\n\n"

输出 :

pack() method form : abcnildog

pack() method form : cowdog