ruby array_Ruby中带有示例的Array.sample()方法

ruby array

Array.sample()方法 (Array.sample() Method)

In this article, we will study about Array.sample() method. You all must be thinking the method must be doing something which is quite different from all those methods which we have studied. 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.sample()方法 。 大家都必须认为该方法必须做的事情与我们研究的所有这些方法截然不同。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

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 chooses a random object let say r from the elements of Array instance. If you are providing a number 'n' along with the method, then the method will return an Array instance which will be of length 'n' and contains unrepeated and unique random numbers. If the method is invoked without providing any parameter, the method will return 'nil' if the Array instance is found to be empty and an empty Array object is returned if the method is invoked with the argument with the Array which has no elements.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是,从Array实例的元素中选择一个随机对象,例如r。 如果与方法一起提供数字“ n” ,则该方法将返回一个数组实例,该实例的长度为“ n”,并且包含未重复且唯一的随机数。 如果在没有提供任何参数的情况下调用该方法,则如果发现Array实例为空,则该方法将返回“ nil”;如果使用带有不带任何元素的Array的参数调用该方法,则该方法将返回空Array对象。

Syntax:

句法:

    array_instance.sample -> object
or
array_instance.sample(n)-> new_array

Argument(s) required:

所需参数:

This method takes one argument which decides the length of the Array instance which is returned by the method.

此方法采用一个参数,该参数确定该方法返回的Array实例的长度。

Example 1:

范例1:

=begin
Ruby program to demonstrate sample method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array sample implementation"
rn = table.sample
puts "The random object generated from Array instance is #{rn}"

Output

输出量

RUN 1:
Array sample implementation
The random object generated from Array instance is 8
RUN 2:
Array sample implementation
The random object generated from Array instance is 18

Explanation:

说明:

In the above code, you can observe that we are generating random numbers from the Array instance with the help of Array.sample() method. In both the Runs, you can observe that the random numbers being generated are totally different from each other.

在上面的代码中,您可以观察到借助于Array.sample()方法 ,我们正在从Array实例生成随机数。 在两次运行中,您都可以观察到生成的随机数彼此完全不同。

Example 2:

范例2:

=begin
Ruby program to demonstrate sample method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array sample implementation"
puts "Enter the number of objects you want to generate:"
num = gets.chomp.to_i
rn = table.sample(num)
puts "The random objects generated from Array instance is #{rn}"

Output

输出量

RUN 1:
Array sample implementation
Enter the number of objects you want to generate:
3
The random objects generated from Array instance is [20, 18, 2]
RUN 2:
Array sample implementation
Enter the number of objects you want to generate:
3
The random objects generated from Array instance is [2, 6, 8]

Explanation:

说明:

In the above code, you can observe that we are generating an Array instance of Random numbers from the Array instance with which Array.sample() method has been invoked. In both the Runs, you can see that the Array instances obtained are totally different from each other.

在上面的代码中,您可以观察到我们正在从调用Array.sample()方法的Array实例生成随机数的Array实例。 在这两次运行中,您都可以看到获得的Array实例彼此完全不同。

翻译自: https://www.includehelp.com/ruby/array-sample-method-with-example.aspx

ruby array

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544176.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ThreadLocal夺命11连问

前言前一段时间,有同事使用ThreadLocal踩坑了,正好引起了我的兴趣。所以近期,我抽空把ThreadLocal的源码再研究了一下,越看越有意思,发现里面的东西还真不少。我把精华浓缩了一下,汇集成了下面11个问题&…

关于静态库、动态库的区别汇总

real framework中不可以使用类别 或 不可以不包含类文件real framework 中直接调用NSClassFromString函数会返回null 需要强制加载指定类 或 直接通过类名引用linux中静态库和动态库的区别一、不同库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行…

PHP array_pad()函数与示例

PHP array_pad()函数 (PHP array_pad() function) array_pad() function is used to pad an array to given size with a specified value and returns a new array with a specified value. array_pad()函数用于将数组填充到具有指定值的给定大小,并返回具有指定值…

Spring Boot 优雅配置多数据源

大约在19年的这个时候,老同事公司在做医疗系统,需要和HIS系统对接一些信息,比如患者、医护、医嘱、科室等信息。但是起初并不知道如何与HIS无缝对接,于是向我取经。最终经过讨论采用了视图对接的方式,大致就是HIS系统提…

(转)新ITC提交APP常见问题与解决方法(Icon Alpha,Build version,AppIcon120x120)(2014-11-17)...

1)ICON无法上传,提示图片透明(有Alpha通道)苹果现在不接受png里的Alpha了,提交的图标带有Alpha通道就提示:简单处理:用自带的预览打开,导出时不勾选Alpha,仍保存为png格式…

python 向量取整数_随机整数向量| 使用Python的线性代数

python 向量取整数Prerequisite: 先决条件: Defining a Vector using list 使用列表定义向量 Defining Vector using Numpy 使用Numpy定义向量 Random Integer Vector is a type of vector having a random integer value at each entry. Such types of vectors ha…

Spring 夺命 35 问!

有人说,“Java程序员都是Spring程序员”,可以看出Spring在Java世界里举足轻重的作用。基础1.Spring是什么?特性?有哪些模块?Spring Logo一句话概括:Spring 是一个轻量级、非入侵式的控制反转 (IoC) 和面向切…

Android百度地图开发03之地图控制 + 定位

前两篇关于百度地图的blog写的是,一些基本图层的展示 和 覆盖物的添加地理编码和反地理编码。 接下来,这篇blog主要说一些关于地图控制方面的内容和定位功能。 百度地图提供的关于地图的操作主要有:单击、双击、长按、缩放、旋转、俯视等。 地…

软件工程需要学什么_为什么我们需要软件工程?

软件工程需要学什么Software engineering is the application of the set of pre-defined procedures while developing any project. But why do we need Software engineering? What factors made us implement these predefined set of procedures and protocols while dev…

IDEA 版 Postman 面世了,功能真心强大!

IDEA是最常用的开发工具,很多程序员都想把它打造成一站式开发平台,于是安装了各种各样的插件。最近发现了一款IDEA插件RestfulFastRequest,细节做的真心不错,说它是IDEA版的Postman也不为过,推荐给大家!Res…

DNS子域授权

转载于:https://blog.51cto.com/changeflyhigh/1697257

mongo数据库插入数据_深入研究Mongo数据库

mongo数据库插入数据More popularly known as "mongoDB". It is a no-sql based database. 俗称“ mongoDB” 。 这是一个基于无SQL的数据库。 BASIC STRUCTURE OF MONGO DB MONGO DB的基本结构 A COLLECTION IN MONGODB having 3 DOCUMENTS MONGODB中有3个文档的集…

java方法重载和重载方法_我们可以在Java中重载main()方法吗?

java方法重载和重载方法The question is that "can we overload main() method in Java?" 问题是“我们可以在Java中重载main()方法吗?” Yes, We can overload the main() method in Java. 是的,我们可以重载Java中的main()方法 。 JVM cal…

五分钟,手撸一个Spring容器!

Spring是我们最常用的开源框架,经过多年发展,Spring已经发展成枝繁叶茂的大树,让我们难以窥其全貌。这节,我们回归Spring的本质,五分钟手撸一个Spring容器,揭开Spring神秘的面纱!从什么是IOC开始…

一步一步搭建客服系统 (7) 多人共享的电子白板、画板

多人共享、同时操作的电子白板,让不同的参入者以不同的颜色来画画;可以保存当前room的内容,以让后来者可以直接加载所有内容。 在github上找到一个用html5 canvas实现的一个电子白板的例子: https://github.com/kblcuk/canvas-whi…

Spring Cloud OpenFeign 的 5 个优化小技巧!

作者 | 磊哥来源 | Java中文社群(ID:javacn666)转载请联系授权(微信ID:GG_Stone)OpenFeign 是 Spring 官方推出的一种声明式服务调用和负载均衡组件。它的出现就是为了替代已经进入停更维护状态的 Feign&am…

java的equals方法_Java Vector equals()方法与示例

java的equals方法向量类的equals()方法 (Vector Class equals() method) equals() method is available in java.util package. equals()方法在java.util包中可用。 equals() method is used to check whether this Vector is the same or equals to the given object (ob) or …

推荐几个好文章

1 cocos2dx各种行动 http://www.cnblogs.com/linux-ios/archive/2013/04/06/3001946.html 2 cocos2dx自己定义曲线 http://blog.csdn.net/ufolr/article/details/7447773 3 lua中载入cocostudio动画,触发帧事件(非常全,非常具体。还有源代码…

treeset java_Java TreeSet first()方法与示例

treeset javaTreeSet类的first()方法 (TreeSet Class first() method) first() method is available in java.util package. first()方法在java.util包中可用。 first() method is used to retrieve the first lowest element in this TreeSet. first()方法用于检索此TreeSet中…

Java常用类:7000字一次性帮你总结好啦!

来源:cnblogs.com/lwtyyds/p/15678152.html常用类概述内部类内部类的分类:1.成员内部类(非静态内部类)2.局部内部类4.匿名内部类Object类Object类常用方法:1.equals方法2.hashCode方法3.toString方法4.finzlize方法包装…