In the last articles, we have gone through many methods through which we can create Array Instances but you all must know that those all were Public class methods and now in the upcoming articles, we will be learning about Public instance methods.

在上一篇文章中,我们介绍了许多可以创建数组实例的方法,但是您必须都知道它们都是Public类方法,现在在接下来的文章中,我们将学习Public实例方法。

Now, you all must be thinking what is the difference between Public class methods and Public instance methods, then to clear your doubt, Public class methods can be invoked as soon as Ruby processes the execution of the class whereas Public instance methods can only be executed after the creation of an Instance of that particular class. We have studied methods like Array.new() which is a resident of Public class methods but now will be studying Public instance methods. Well, in this article, we will be studying about creating the Array instance with the help of & operator.

现在,您必须都在考虑Public类方法和Public实例方法之间的区别,然后为澄清您的疑问,只要Ruby处理类的执行,就可以调用Public类方法,而Public实例方法只能执行在创建该特定类的实例之后。 我们研究了Array.new()之类的方法,该方法是Public类方法的居民,但现在将研究Public实例方法。 好吧,在本文中,我们将研究如何在&运算符的帮助下创建Array实例

Method description:

方法说明:

The & symbol is the way to create an Array object and it requires two previously defined Array instances. It is also known as the Set intersection. It works in the way that it returns a new Array instance having elements that are common to both the Arrays, excluding the duplicate elements present in them. The order is maintained from the original Array.

&符号是创建Array对象的方法,它需要两个先前定义的Array实例。 也称为Set相交。 它的工作方式是返回一个新的Array实例,该实例具有两个数组共同的元素,但不包括其中存在的重复元素。 该顺序是从原始Array维护的。

Syntax:

句法:

    aew_array = old_array1 & old_array2

Parameter(s):

参数:

The & operator takes two arguments and they both are Array instances.

&运算符带有两个参数,它们都是Array实例。

Example 1:

范例1:

=begin
Ruby program to create Array with &
=end
# arrays
old_arr1 = [23,44,66,33,12,77,33]
old_arr2 = [12,23,66,2,44,77,8]
# creating a new array using & operator
new_Arr = old_arr1 & old_arr2
# printing the array
puts "The new Integer Array Instance is:"
print new_Arr

Output

输出量

The new Integer Array Instance is:
[23, 44, 66, 12, 77]

Explanation:

说明:

In the above code, you can observe that we are creating an Array instance with the help of & operator. In the resulting Array, you can see that all the elements are residing which common in both the Array are being passed as the arguments.

在上面的代码中,您可以看到我们正在使用&运算符创建一个Array实例 。 在生成的Array中,您可以看到所有元素都驻留在两个Array中哪个公共位置作为参数传递。

Example 2:

范例2:

=begin
Ruby program to create Array with & operator
=end
# arrays
old_arr1 = ['Ramit','Amit','Suresh','Payal','Samir']
old_arr2 = ['Babita','Amit','Suresh']
# creating a new array using & operator
new_Arr = old_arr1 & old_arr2
# printing the array
puts "The new String Array Instance is:"
print new_Arr

Output

输出量

The new String Array Instance is:
["Amit", "Suresh"]

Explanation:

说明:

In the above code, you can observe that we are creating a String Array with the help of & operator. This operator internally uses .eql? method for faster carrying out a faster checking of Array elements of both the Arrays.

在上面的代码中,您可以观察到我们在&运算符的帮助下创建了一个字符串数组 。 该运算符内部使用.eql吗? 快速执行两个数组的数组元素的快速检查的方法

翻译自: https://www.includehelp.com/ruby/creating-array-instance-with-and-operator-new_array-arr-old_array.aspx

在Ruby中使用&运算符(new_array- arr&old_Array)创建数组实例相关推荐

  1. ruby三元操作符_在Ruby中使用操作符将元素添加到数组实例中

    ruby三元操作符 In the previous articles, we have gone through ways through which we can create Array inst ...

  2. ruby 生成哈希值_哈希== Ruby中的运算符

    ruby 生成哈希值 In the last article, we have seen how we can compare two hash objects with the help of &l ...

  3. ruby 生成哈希值_哈希 Ruby中的运算符

    ruby 生成哈希值 In the last article, we have seen how we can carry out a comparison between two hash obje ...

  4. python类中引用数据是通过_重载python类中的[]运算符以引用数字阵列数据内存

    我已经编写了一个数据容器类,它本质上包含一个numpy-ndarray成员,以及生成时间序列掩码/横截面掩码的方法,在环形缓冲模式下获取日期索引(行#).处理调整大小时要考虑到数据可能是环形缓冲区,以 ...

  5. ruby 集合 分组_在Ruby中打印集合的元素

    ruby 集合 分组 We have gone through the implementation of sets in Ruby. They are very similar to arrays. ...

  6. Java 数组中new Object[5]语句是否创建了5个对象?

    点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/qq_33642117/ article/details/52214403 Java 数组的本质是一个Java类,它是通过new ...

  7. C++中的运算符重载

    1.Cpp中的重载运算符和重载函数 C++允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载.重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但 ...

  8. map(&:name)在Ruby中是什么意思?

    我在RailsCast中找到了以下代码: def tag_names@tag_names || tags.map(&:name).join(' ') end 什么是(&:name)在m ...

  9. ruby 集合 分组_在Ruby中找到两个集合之间的区别

    ruby 集合 分组 Finding differences simply means that finding elements that are uncommon between two sets ...

最新文章

  1. 我的业务要不要用人工智能?引入AI前你需要评估的(四)
  2. Hardfault 2
  3. 在MFC中使用Static text控件显示消息
  4. Python也有做大型游戏的潜力?原来我们小看了Python,无所不能!!
  5. django mysql内存泄漏_Django的:使内存的关系,而不保存到数据库
  6. 【Envi风暴】Envi插件大全:多波段拆分工具的巧妙使用
  7. flag push tcp 作用_TCP协议超详细解析及攻击/防范
  8. 软件测试人员如何在“金三银四”跳槽季找到理想工作?
  9. a链接易混淆与form表单简易验证用法详解
  10. 【优化调度】基于matlab一致性算法求解电力系统分布式经济调度优化问题【含Matlab源码 770期】
  11. 软件测试项目实战步骤
  12. 你必须掌握的人生定律
  13. 搭建网站基本步骤(搭建一个网站的步骤)
  14. 掌财社寒山:俄罗斯银行将在年底前推出数字卢布原型
  15. Lost connection to MySQL server during query的几种可能分析
  16. 资深摄影师眼中,青岛值得一游的景点有哪些?
  17. Trying to resize storage that is not resizeable 解决
  18. “科林明伦杯”哈尔滨理工大学第十届部分题解
  19. 基金使用计划 数学建模 matlab,数学建模论文-基金使用计划
  20. MgH2-grap金属Ni-Mg/C镍对镁碳复合储氢材料hene|Mg/C复合储氢材料复合储氢材料

热门文章

  1. 新手攻略熔炉_我的世界攻略:生存模式新手攻略
  2. ①你真的学会Java了吗?来自《卷Ⅰ》的灵魂提问
  3. PARALLEL(并行)
  4. linux下安装davinci
  5. Ubuntu主题更换
  6. 数据库的辅助工具:My-SqlViewer
  7. CentOS 7.1下KVM的安装与配置
  8. intellij idea 最常用的快捷键
  9. linux md5 加密字符串和文件方法
  10. redhat linux配置RSH遇见的问题