obj[]与obj.

Ruby Array.include?(obj)方法 (Ruby Array.include?(obj) Method)

In the previous articles, we have seen how we can check whether two Array instances are identical or not with the help of <=> operator, == operator, and .eql? method? We have also seen different ways through which we can insert elements in the previously defined Array instances. Now, we can say that we have got a decent amount of knowledge about the Array class in Ruby language. In the last article, we have seen the implementation of the assoc() method but the assoc method only works for the Array object which is the collection of multiple Array objects.

在前面的文章中,我们已经看到如何借助<=>运算符 , ==运算符和.eql来检查两个Array实例是否相同 方法 ? 我们还看到了可以在先前定义的Array实例中插入元素的不同方法。 现在,可以说我们已经对Ruby语言中的Array类有了相当多的了解。 在上一篇文章中,我们看到了assoc()方法的实现,但是assoc方法仅适用于Array对象,后者是多个Array对象的集合。

For normal Array objects, we have Array.include?(obj) method. In this article, we will see how we can implement Array.include?() method? We will go through its syntax and some examples in the rest of the Array.

对于普通的Array对象,我们有Array.include?(obj)方法 。 在本文中,我们将看到如何实现Array.include?()方法 ? 我们将在数组的其余部分中介绍其语法和一些示例。

Method description:

方法说明:

This method is a Public instance method and belongs to the Array class which lives inside the library of Ruby language. This method is used to check whether an object is a part of the particular Array instance or not. It will search through the whole Array and gives you the result according to its search. Let us go through the syntax and demonstrating the program codes of this method.

该方法是Public实例方法,属于Array类,该类位于Ruby语言库中。 此方法用于检查对象是否为特定Array实例的一部分。 它将搜索整个数组,并根据其搜索结果。 让我们来看一下语法,并演示该方法的程序代码。

If you are thinking about what it will return then let me tell you, it will return a Boolean value. The returned value will be true if it finds the object inside the Array and the return value will be false if it does not find the object to be the part of the Array instance.

如果您正在考虑它将返回什么,那么让我告诉您,它将返回一个布尔值。 如果在数组内找到对象,则返回值将为true;如果找不到对象是Array实例的一部分,则返回值为false。

Syntax:

句法:

    array_instance.include?(obj)

Parameter(s):

参数:

This method only takes one parameter and that argument is nothing but an object whose presence we want to check.

此方法仅使用一个参数,而该参数不过是一个要检查其存在性的对象。

Example 1:

范例1:

=begin
Ruby program to demonstrate include method
=end
# array
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
# input element to search
puts "Enter the element you want to search"
ele = gets.chomp
# checking
if array1.include?(ele) != false
puts "Element found"
else
puts "Element not found"
end

Output

输出量

RUN 1:
Enter the element you want to search
Apple
Element found
RUN 2:
Enter the element you want to search
Mango
Element not found

Explanation:

说明:

In the above code, you can observe that we are invoking the include method on the normal Array instance. It has returned true when it found the presence of an object in the Array object which is entered by the user.

在上面的代码中,您可以观察到我们在普通Array实例上调用include方法。 当发现用户输入的Array对象中存在对象时,它返回true。

Example 2:

范例2:

=begin
Ruby program to demonstrate include method
=end
# arrays
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
array2 = ["Akul","Madhu","Ashok","Mukesh",788]
array3 = ["Orange","Banana","Papaya","Apricot","Grapes"]
# main array
arraymain = [array1,array2,array3]
# input element to search
puts "Enter the element you want to search"
ele = gets.chomp
# checking
if arraymain.include?(ele) != false
puts "Element found"
else
puts "Element not found"
end

Output

输出量

Enter the element you want to search
Array1
Element not found

Explanation:

说明:

In the above, you can verify that include method does not work upon Array instance which is the collection of multiple Array instances. It will return false even if the object is a part of the Array instance.

在上面,您可以验证include方法不适用于Array实例,该实例是多​​个Array实例的集合。 即使对象是Array实例的一部分,它也会返回false。

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

obj[]与obj.

obj[]与obj._Ruby中带有示例的Array.include?(obj)方法相关推荐

  1. obj[]与obj._Ruby中带有示例的Array.rassoc(obj)方法

    obj[]与obj. Array.rassoc(obj)方法 (Array.rassoc(obj) Method) In this article, we will study about Array ...

  2. ruby hash方法_Ruby中带有示例的Hash.rassoc(obj)方法

    ruby hash方法 Hash.rassoc(obj)方法 (Hash.rassoc(obj) Method) In this article, we will study about Hash.r ...

  3. ruby hash方法_Ruby中带有示例的Hash.invert方法

    ruby hash方法 Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert Meth ...

  4. ruby hash方法_Ruby中带有示例的Hash.select方法

    ruby hash方法 哈希选择方法 (Hash.select Method) In this article, we will study about Hash.select Method. The ...

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

    ruby array Array.cycle()方法 (Array.cycle() Method) In this article, we will study about Array.cycle() ...

  6. ruby array_Ruby中带有示例的Array.fill()方法(1)

    ruby array Array.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() me ...

  7. array.slice_Ruby中带有示例的Array.slice()方法

    array.slice Array.slice()方法 (Array.slice() Method) In this article, we will study about Array.slice( ...

  8. ruby array_Ruby中带有示例的Array.shuffle方法

    ruby array Array.shuffle方法 (Array.shuffle Method) In this article, we will study about Array.shuffle ...

  9. repeated_Ruby中带有示例的Array.repeated_combination()方法

    repeated Array.repeated_combination()方法 (Array.repeated_combination() Method) In this article, we wi ...

最新文章

  1. C#中如何获取注册表信息
  2. 走在专家的路上,每天一条SQL优化(3)
  3. 简易的阻尼滚动条的插件
  4. Qt访问SQLite数据库
  5. (原)Eclipse中将JNI生成的so打包成jar的步骤
  6. crontab定时任务运行
  7. 36.LEN() 函数
  8. set vector_Java Vector set()方法与示例
  9. CSS基础学习 18.CSS多列
  10. mysql jooyoo net_.net连接MySQL的方法
  11. “我不看春晚,但想要张小斐同款”
  12. 微信里文件小程序导不出来_懒得打开电脑传文件?这四类小程序把这事解决了-小程序视频怎么发在电脑上...
  13. 中央推进城镇化建设 六行业分享25万亿蛋糕
  14. latex 琐粹记录
  15. 一个善意的谎言拯救一个团队 (又叫沙漠中的指南针)
  16. Scratch编程-画图模块13【寿光市青少年创意编程大赛真题】
  17. Mogafx英美对有望结束连续两日的跌势
  18. 2020语数英小学初中课程百度云学而思Python笔记
  19. 康涅狄格大学计算机科学排名,2019上海软科世界一流学科排名计算机科学与工程专业排名康涅狄格大学排名第401-500...
  20. zynq+ad9361 petalinux使用官方IIO示波器调试记录

热门文章

  1. html图片拼接出现留白,关于img标签周围留白的问题
  2. java异常标记_java.lang.RuntimeException:错误:0D0680A8:asn1编码例程:ASN1_CHECK_TLEN:错误的标记...
  3. docker run 挂载卷_docker mysql配置挂载到卷
  4. ubuntu 安装java jdk_「ubuntu安装jdk」Ubuntu安装jdk8的两种方式 - seo实验室
  5. 主动断开socket链接_TCP连接与断开详解(socket通信)
  6. localdatetime 默认时间_java中的时间与时区:LocalDateTime和Date
  7. Go实现简单的RESTful_API
  8. Ubuntu16.04LTS修改开机动画
  9. 360 再次开源管理平台 Wayne:基于企业级 Kubernetes 集群
  10. Day31 python基础--网络编程基础-socketserver