ruby hash方法_Hash.insert()方法以及Ruby中的示例

ruby hash方法

Hash.insert()方法 (Hash.insert() Method)

In this article, we will study about Hash.insert() Method. You all must be thinking the method must be doing something which is related to the insertion of certain element. 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.

在本文中,我们将研究Hash.insert()方法 。 你们都必须认为该方法必须执行与插入某些元素有关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is one of the examples of the Public instance method which is specially defined in the Ruby library for Array class. This method works in the way that it inserts the object before the element with the given index. If you provide the negative index then it will count the index from the backward of the Array instance. This method is one of the examples of destructive methods where the changes made by these methods are permanent. There is no non-destructive version of this method.

此方法是Ruby类库中为Array类专门定义的Public实例方法的示例之一。 此方法的工作方式是将对象插入具有给定索引的元素之前。 如果提供负索引,则它将从Array实例的后面开始计算索引。 此方法是破坏性方法的示例之一,这些方法所进行的更改是永久性的。 没有此方法的非破坏性版本。

Syntax:

句法:

    Array_instance.insert(index,object)

Argument(s) required:

所需参数:

This method accepts two arguments. The first one is the index where you want to insert the object of any class and the second word is the String having the name of the object. These two arguments are mandatory as their absence will make you see Exception. The number of arguments can be more as well but can never be less than 2.

此方法接受两个参数。 第一个是要在其中插入任何类的对象的索引,第二个单词是具有对象名称的String。 这两个参数是强制性的,因为它们的缺失会使您看到Exception。 参数的数量也可以更多,但不能少于2。

Example 1:

范例1:

=begin
  Ruby program to demonstrate insert method
=end	

lang = ["C++","Java","Python","Ruby","Perl"]

puts "Array insert implementation."

puts "Enter the index where you want to insert:"
ind = gets.chomp.to_i

puts "Enter the object which you want to insert:"
ele = gets.chomp

if(ind = lang.insert(ind,ele))
	puts "Object inserted properly"
else
	puts "Error in inserting object"
end

puts "Array elements are:"
print lang

Output

输出量

Array insert implementation.
Enter the index where you want to insert:
 2
Enter the object which you want to insert:
 HTML
Object inserted properly
Array elements are:
["C++", "Java", "HTML", "Python", "Ruby", "Perl"]

Explanation:

说明:

In the above code, you can observe that we have asked the user for the index where she wants to insert the object and the name of the object. In the last part of output where the whole Array object is printed, you can observe that we have "HTML" at that index only.

在上面的代码中,您可以观察到我们已经询问用户要在哪里插入对象的索引以及对象的名称。 在输出了整个Array对象的输出的最后一部分,您可以观察到我们仅在该索引处具有“ HTML”

Example 2:

范例2:

=begin
  Ruby program to demonstrate insert method
=end	

lang = ["C++","Java","Python","Ruby","Perl"]

puts "Array insert implementation."

puts "Enter the  first index where you want to insert:"
ind1 = gets.chomp.to_i

puts "Enter the first object which you want to insert:"
ele1 = gets.chomp

puts "Enter the  second object where you want to insert:"
ele2 = gets.chomp

puts "Enter the third object which you want to insert:"
ele3 = gets.chomp

if(ind = lang.insert(ind1,ele1,ele2,ele3))
	puts "Object inserted properly"
else
	puts "Error in inserting object"
end

puts "Array elements are:"
print lang

Output

输出量

Array insert implementation.
Enter the  first index where you want to insert:
 2
Enter the first object which you want to insert:
 Java
Enter the  second object where you want to insert:
 Javascript
Enter the third object which you want to insert:
 COBOL
Object inserted properly
Array elements are:
["C++", "Java", "Java", "Javascript", "COBOL", "Python", "Ruby", "Perl"]

Explanation:

说明:

In the above code, you can see that we inserting several elements at the same time in the Array instance. We have asked the user for the index from where she wants to start inserting the objects. The first object is inserted at that location and others are inserted at the consecutive locations.

在上面的代码中,您可以看到我们在Array实例中同时插入了几个元素。 我们已经询问用户要从哪里开始插入对象的索引。 第一个对象插入到该位置,其他对象插入到连续位置。

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

ruby hash方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值