ruby三元操作符

In the previous articles, we have gone through ways through which we can create Array instances. Some of them were Public instance methods and some were Public class methods. We should also know how they both differ from each other. Now we know multiple ways through which we can declare or generate our Array instances. Some are direct bypassing some arguments and some with the help of previously defined Array objects. Now, we will learn how we can add some elements to the previously defined Array? In this article, we will be learning about << with the help of which we can add elements to the instances of Array class.

在之前的文章中,我们介绍了创建Array实例的方法。 其中一些是Public实例方法,有些是Public类方法。 我们还应该知道它们彼此之间有何不同。 现在我们知道了多种方法来声明或生成Array实例。 有些直接绕过某些参数,有些则借助先前定义的Array对象。 现在,我们将学习如何向先前定义的数组添加一些元素? 在本文中,我们将学习<< ,我们可以借助<<将元素添加到Array类的实例。

Method description:

方法说明:

This is a public instance method. As discussed above, this method is used to add elements in a previously declared object of the Array class. This method works in a way that pushes the object to the end of the Array instance which is passed as the parameter to this symbol. This is a destructive method by nature as the changes created by this method are permanent and can't be changed later.

这是一个公共实例方法。 如上所述,此方法用于在Array类的先前声明的对象中添加元素。 此方法的工作方式是将对象推送到Array实例的末尾,该实例作为参数传递给此符号。 本质上,这是一种破坏性方法,因为此方法创建的更改是永久性的,以后无法更改。

Syntax:

句法:

    array_instance << object

Parameter(s):

参数:

This method takes only one parameter which is the instance of Array and it is passed at the left-hand side of the operator or method.

此方法仅使用一个参数(它是Array的实例),并在运算符或方法的左侧传递。

Example 1:

范例1:

=begin
Ruby program to add an Array to Another
with the help of <<
=end
# array declaration
old_arr1 = ['Payal','Samir','Sonakshi','Hira','Panna']
# adding elements
old_arr1 << 'Garvit'
old_arr1 << 'Monika'
old_arr1 << 'Anushree'
# printing the array
puts "The new String Array Instance is:"
print old_arr1

Output

输出量

The new String Array Instance is:
["Payal", "Samir", "Sonakshi", "Hira", "Panna", "Garvit", "Monika", "Anushree"]

Explanation:

说明:

In the above code, you can observe that we are pushing or adding a String class object at the end of the Array instance which is passed as the parameter to the << operator or method. At the last when we are printing the Array object then you can observe the reflection of that object in the Array instance.

在上面的代码中,您可以观察到我们在Array实例的末尾推入或添加String类对象,该对象作为参数传递给<<操作符或方法 。 最后,当我们打印Array对象时,您可以观察到该对象在Array实例中的反射。

Example 2:

范例2:

=begin
Ruby program to add an Array to Another
with the help of <<
=end
# array declarations
old_arr1 = ['Ramit','Amit','Suresh','Payal']
old_arr2 = ['Payal','Samir','Sonakshi','Hira','Panna']
# adding elements of old_arr2 to old_arr1
old_arr1 << old_arr2
# printing array elements
puts "The new String Array Instance is: "
print old_arr1

Output

输出量

The new String Array Instance is:
["Ramit", "Amit", "Suresh", "Payal", ["Payal", "Samir", "Sonakshi", "Hira", "Panna"]]

Explanation:

说明:

In the above code, you can observe that we are adding or pushing an Array instance to the end of another Array. Now our second Array is residing in the first Array at the last index. So, it can be accessed with the help of the last index only.

在上面的代码中,您可以观察到我们正在将Array实例添加或推入另一个Array的末尾。 现在我们的第二个数组位于最后一个索引的第一个数组中。 因此,只能在最后一个索引的帮助下进行访问。

翻译自: https://www.includehelp.com/ruby/adding-elements-into-an-array-instance-with-left-shift-operator.aspx

ruby三元操作符

ruby三元操作符_在Ruby中使用操作符将元素添加到数组实例中相关推荐

  1. C语言fgets()函数(以指定长度读取文件中的字符,并存入字符数组变量中)

    C语言fgets()函数(以指定长度读取文件中的字符,并存入字符数组变量中) 需要引入C 标准库 - <stdio.h> 文章目录 描述 声明 参数 返回值 实例 测试(确实只能读n-1个 ...

  2. HTML中如何给HTML元素添加事件

    HTML中如何给HTML元素添加事件 方法一: 代码示例: <!DOCTYPE html> <html><head><meta charset="U ...

  3. [C语言]指针之数组逆序函数:编写函数invert,将数组中的n个整数按相反顺序存放,要求用指针变量作为函数形参,并用指针的方法遍历该数组。在main函数中输入n个整数,存入数组a中;然后调用上述函

    编写函数invert,将数组中的n个整数按相反顺序存放,要求用指针变量作为函数形参,并用指针的方法遍历该数组. 在main函数中输入n个整数,存入数组a中:然后调用上述函数处理数组a,最后逐个输出数组 ...

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

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

  5. ruby 集合 分组_将Ruby中两个集合的所有元素结合在一起

    ruby 集合 分组 In this program, we will see how we can combine the two sets? This is not a very difficul ...

  6. 地壳中元素含量排名记忆口诀_地壳中含量最多的元素是什么?地壳中元素含量排名口诀...

    地壳,乃地质术语,是指由岩石组成的固体地壳,是地球固体圈的最外层,也是岩石圈的重要组成部分.那么地壳中含量最多的元素是什么呢?以及地壳中元素含量排名口诀又是什么呢? 地壳中含量最多的元素 从地震波的研 ...

  7. maxN - 返回数组中N个最大元素 minN - 返回数组中N个最小元素

    从提供的数组中返回 n 个最小元素.如果 n 大于或等于提供的数组长度,则返回原数组(按降序排列). 结合使用Array.sort() 与展开操作符(...) ,创建一个数组的浅克隆,并按降序排列. ...

  8. ajax在项目中怎么使用,我如何添加项目在sql中使用jQuery(ajax)通过web服务

    我有一个web服务,并在其中有两种方法(select,insertdata).我想用jquery在sql中插入一条记录.我怎样才能做到这一点?我已经制作了该代码,但它不起作用.请帮助我.我如何添加项目 ...

  9. js中怎么为同级元素添加点击事件

    事件件是javascript脚本语言的重要组成部分,因为有事件才使用户页面的体验更加的美好.元素添加事件是js语言中最基础的.我们可以为元素本身添加事件,也可以通过事件绑定和事件监听为元素的父元素和子 ...

最新文章

  1. php closure invoke,PHP Closure类详解
  2. 首席Execution官
  3. 代码整洁之道--思维导图
  4. python需要的基础_推荐收藏!小白不要怕!一周学全Python面试基础(2)
  5. tensorflow--filter、strides
  6. java mysql_num_rows_JAVA MYSQL sql_calc_found_rows和found_rows()实践
  7. [Python] 关键字 yield 用法详解
  8. 火狐浏览器驱动_火狐浏览器开始支持比WebGL更简单的绘图API WebGPU
  9. (转)对冲基金投身“另类数据”淘金热
  10. c语言坐标轮换法_优化设计-鲍威尔法程序(c语言)
  11. 导航条UINavigtionBar,标签栏UITabBarController,抽屉MMDrawerController
  12. 靶向肿瘤代谢,助力攻克癌症
  13. 如何制作Android.9图片
  14. OpenWrt设置修改IP地址
  15. Python爬懂车帝的图片-代码
  16. JAVA 生成二维码并保存到本地或文件服务器
  17. 【环信IM集成指南】iOS端常见问题整理(2)
  18. Idea编译出现[ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter这个问题
  19. 中国制造2025-智能制造是强国必由之路
  20. 几个常见的B端推广渠道

热门文章

  1. axios安装_Vue脚手架安装,与基本语法(干货)
  2. Docker使用-Hello World
  3. 问题 I: 连通块计数
  4. 【实时+排重】摆脱渠道统计刷量作弊行为
  5. Linux的启动流程简析(以Debian为例)
  6. 仿拉钩app(一)---爬虫数据准备
  7. 第二个冲刺期的第六天
  8. debugging Auto Layout:Logical Errors
  9. 一个简单的封ip规则
  10. Linux解析内核源代码——传输控制块诞生