博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ruby array_Array.shuffle! Ruby中的示例方法
阅读量:2531 次
发布时间:2019-05-11

本文共 4048 字,大约阅读时间需要 13 分钟。

ruby array

Array.shuffle! 方法 (Array.shuffle! Method)

In this article, we will study about Array.shuffle! method. You all must be thinking the method must be doing something related to the shuffling of elements or objects in 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.shuffle! 方法 。 你们都必须认为该方法必须执行与Array实例中的元素或对象的改组有关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is a public instance method and defined for the Array class in Ruby's library. This method works in such a way that it shuffles the objects present in the Array instance randomly. The return type of this method is an Array object which contains all the elements of self in a shuffled manner. You can also provide an optional argument rng which can be used as a random number generator. This method is one of the examples of destructive method which means that the changes created by this method are permanent or non-temporary and would impact the actual arrangement of elements in the self Array instance.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 该方法的工作方式是随机地对Array实例中存在的对象进行洗牌。 此方法的返回类型是一个Array对象,它以随机的方式包含self的所有元素。 您还可以提供一个可选参数rng ,它可以用作随机数生成器 。 此方法是破坏性方法示例之一,这意味着此方法创建的更改是永久的或非临时的,并且会影响self Array实例中元素的实际排列。

Syntax:

句法:

array_instance.shuffle! -> new_array    or    array_instance.shuffle!(random:rng)-> new_array

Argument(s) required:

所需参数:

This method takes one argument which is optional. This argument can be used for random number generation.

此方法采用一个可选参数。 此参数可用于生成随机数

Example 1:

范例1:

=begin  Ruby program to demonstrate shuffle! method=end# array declarationtable = [2,4,6,8,10,12,14,16,18,20]puts "Array shuffle! implementation"pq =table.shuffle!puts "Array instance after shuffling: #{pq}"puts "Array instance:"print table

Output

输出量

RUN 1:Array shuffle! implementationArray instance after shuffling: [16, 6, 4, 2, 8, 12, 14, 10, 20, 18]Array instance:[16, 6, 4, 2, 8, 12, 14, 10, 20, 18]RUN 2:Array shuffle! implementationArray instance after shuffling: [12, 2, 14, 4, 6, 20, 10, 8, 18, 16]Array instance:[12, 2, 14, 4, 6, 20, 10, 8, 18, 16]

Explanation:

说明:

In the above code, you can observe that we are shuffling the elements from the Array instance with the help of Array.shuffle! method. You can observe that in both the runs, the output or the Array instance generated is different because the shuffling of the elements is always random. You can also see that the elements in self Array are also changing because this method is one of the examples of the destructive methods.

在上面的代码中,您可以观察到我们正在借助Array.shuffle将Array实例中的元素改组! 方法 。 您可以观察到,在两次运行中,由于元素的改组始终是随机的,因此输出或生成的Array实例是不同的。 您还可以看到self Array中的元素也在发生变化,因为此方法是破坏性方法的示例之一。

Example 2:

范例2:

=begin  Ruby program to demonstrate shuffle! method=end# array declarationtable = [2,4,6,8,10,12,14,16,18,20]puts "Array shuffle! implementation"pq =table.shuffle!(random: Random.new(2))puts "Array instance after shuffling: #{pq}"puts "Array instance:"print table

Output

输出量

RUN 1:Array shuffle! implementationArray instance after shuffling: [10, 4, 12, 2, 16, 6, 8, 14, 20, 18]Array instance:[10, 4, 12, 2, 16, 6, 8, 14, 20, 18]RUN 2:Array shuffle! implementationArray instance after shuffling: [10, 4, 12, 2, 16, 6, 8, 14, 20, 18]Array instance:[10, 4, 12, 2, 16, 6, 8, 14, 20, 18]

Explanation:

说明:

In the above code, you can observe that we are shuffling the elements of the Array instance with the help of Array.shuffle method. We are passing an argument inside the method in order to generate a random number. This helps you in the way that it makes the shuffling constant. In both the runs, you can observe that the returning Array is constant. This method is a destructive method that is why it is creating changes in the actual array instance.

在上面的代码中,您可以观察到我们正在借助Array.shuffle方法对Array实例的元素进行混洗 。 我们在方法内部传递参数以生成随机数。 这以使改组为常数的方式帮助您。 在这两次运行中,您都可以观察到返回的Array是常量。 此方法是一种破坏性方法,因此它要在实际的数组实例中创建更改。

翻译自:

ruby array

转载地址:http://usvzd.baihongyu.com/

你可能感兴趣的文章
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>
react.js
查看>>
P1313 计算系数
查看>>
NSString的长度比较方法(一)
查看>>
Azure云服务托管恶意软件
查看>>
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>
旧的起点(开园说明)
查看>>
生产订单“生产线别”带入生产入库单
查看>>
crontab导致磁盘空间满问题的解决
查看>>
java基础 第十一章(多态、抽象类、接口、包装类、String)
查看>>
Hadoop 服务器配置的副本数量 管不了客户端
查看>>
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>