ruby array_Ruby中带有示例的Array.pack()方法
ruby array
Array.pack()方法 (Array.pack() Method)
In this article, we will study about Array.pack() Method. You all must be thinking the method must be doing something which is related to finding values from the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.
在本文中,我们将研究Array.pack()方法 。 你们都必须认为该方法必须在做一些与从Array实例中查找值有关的事情。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。
Method description:
方法说明:
This method is a public instance method and defined for the Array class in Ruby’s library. This method works in a way that it packs the objects of Array instance into a binary sequence. This sequence depends upon the directives present in the aTemplateString. These directives can be “A”, “a” or “Z” which can be followed by count which provides you the width of the resulting field. The directives which are left can also take a count which indicates the number of Array elements to be converted. If the count is an (“*“) then all the elements will be converted.
该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是将Array实例的对象打包为二进制序列。 此序列取决于aTemplateString中存在的指令。 这些伪指令可以是“ A”,“ a”或“ Z” ,其后可以跟count(计数),count为您提供结果字段的宽度。 剩下的指令也可以进行计数,该计数指示要转换的Array元素的数量。 如果计数为( “ *” ),则将转换所有元素。
Syntax:
句法:
array_instance.pack(aTemplateString) -> aBinaryString
Argument(s) required:
所需参数:
This method takes a template string as an argument. You can provide atmost one template String.
此方法将模板字符串作为参数。 您最多可以提供一个模板字符串。
Example 1:
范例1:
=begin
Ruby program to demonstrate pack method
=end
# array declaration
table = ["Geeta","Sabita","Monica","Syresh","Kamish","Punish"]
puts "Array pack implementation"
puts table.pack("A3A3A3A3A3A3")
puts "Array instance : #{table}"
Output
输出量
Array pack implementation
GeeSabMonSyrKamPun
Array instance : ["Geeta", "Sabita", "Monica", "Syresh", "Kamish", "Punish"]
Explanation:
说明:
In the above code, you can observe that we are creating Binary String from the Array instance with the help of the Array.pack() method. The method is returning a String which has been packed using TemplateString “A3A3A3A3A3A3”.
在上面的代码中,您可以观察到我们是在Array.pack()方法的帮助下从Array实例创建Binary String的。 该方法返回一个使用TemplateString “ A3A3A3A3A3A3A3”打包的String。
Example 2:
范例2:
=begin
Ruby program to demonstrate pack method
=end
# array declaration
table = ["fatima","Sabita","Monica","Syresh","Kamish","Punish"]
puts "Array pack implementation"
puts table.pack("a3a3a3")
puts "Array instance : #{table}"
Output
输出量
Array pack implementation
fatSabMon
Array instance : ["fatima", "Sabita", "Monica", "Syresh", "Kamish", "Punish"]
Explanation:
说明:
In the above code, you can observe that we are creating Binary String from the Array instance with the help of the Array.pack() method. The method is returning a String which has been packed using TemplateString “a3a3a3”.
在上面的代码中,您可以观察到我们是在Array.pack()方法的帮助下从Array实例创建Binary String的。 该方法返回一个使用TemplateString “ a3a3a3”打包的String。
翻译自: https://www.includehelp.com/ruby/array-pack-method-with-example.aspx
ruby array
还没有评论,来说两句吧...