ruby replace_Ruby中带有示例的Array.replace()方法

ruby replace

Array.replace()方法 (Array.replace() Method)

In this article, we will study about Array.replace() method. You all must be thinking the method must be doing something related to replacing the Array instance with something. 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.replace()方法 。 你们都必须认为该方法必须在做一些与用某些实例替换Array实例有关的事情。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

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 is used to replace the Array instance with another Array instance. This method works in the way that it replaces the content of self with the elements of another Array. It may truncate it or expand it as well. 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实例替换为另一个Array实例。 此方法的工作方式是将self的内容替换为另一个Array的元素。 它可能会截断或扩展它。 此方法是破坏性方法的示例之一,这些方法所进行的更改是永久性的。 没有此方法的非破坏性版本。

Syntax:

句法:

    array_instance.replace(array_instance)

Argument(s) required:

所需参数:

This method takes an array instance as the parameter. It will give you an exception if you do not provide any array instance.

此方法将数组实例作为参数。 如果您不提供任何数组实例,它将给您一个例外。

Example 1:

范例1:

=begin
  Ruby program to demonstrate replace method
=end

# array declaration
Heart = ["Unconfident","Sad","No friends","Trust issues","Passing days"]

puts "Array replace implementation."
squad = ["Saksham","Nikhil","Ayush","Amisha","Satyam","Living days"]

Heart.replace(squad)

puts "Array elements are:"
print Heart

Output

输出量

Array replace implementation.
Array elements are:
["Saksham", "Nikhil", "Ayush", "Amisha", "Satyam", "Living days"]

Explanation:

说明:

In the above code, you can see that we are replacing the elements of Heart array with the elements of squad array. This method is a destructive method as changes made by it are permanent.

在上面的代码中,您可以看到我们将Heart数组的元素替换为squad数组的元素。 此方法是一种破坏性方法,因为它所做的更改是永久性的。

Example 2:

范例2:

=begin
  Ruby program to demonstrate replace method
=end

# array declaration
Heart = ["Unconfident","Sad","No friends","Trust issues","Passing days"]

puts "Array replace implementation."
Heart.replace(["Saksham","Nikhil","Ayush","Amisha","Satyam","Living days"])

puts "Array elements are:"
print Heart

Output

输出量

Array replace implementation.
Array elements are:
["Saksham", "Nikhil", "Ayush", "Amisha", "Satyam", "Living days"]

Explanation:

说明:

In the above code, you can see that we are replacing the elements of Heart array with the elements of squad array. This method is destructive as changes made by it are permanent. It is not necessary that you need to declare an Array instance first then pass into the method. You can directly pass elements as shown in the above example.

在上面的代码中,您可以看到我们将Heart数组的元素替换为squad数组的元素。 此方法具有永久性,因此具有破坏性。 不必先声明一个Array实例然后传递给该方法。 您可以直接传递元素,如上面的示例所示。

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

ruby replace

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值