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

ruby array

Array.join()方法 (Array.join() Method)

In this article, we will study about Array.join() method. You all must be thinking the method must be doing something which is related to joining 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.join()方法 。 你们都必须认为该方法必须在做一些与将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 join the Array instance with some symbol passed as the argument. This method works in the way that it joins the content of self with the symbol or if you don’t provide any symbol then it simply joins the elements and converts them into a String. The return type of this method is String. This method is one of the examples of non-destructive methods where the changes made by these methods are permanent. There is no destructive version of this method.

此方法是Ruby类库中为Array类专门定义的Public实例方法的示例之一。 此方法用于通过传递一些符号作为参数来连接Array实例。 此方法的工作方式是将self的内容与符号连接在一起,或者,如果您不提供任何符号,则只需将元素连接并将其转换为String。 该方法的返回类型为String。 此方法是非破坏性方法的示例之一,其中通过这些方法所做的更改是永久性的。 此方法没有破坏性版本。

Syntax:

句法:

    array_instance.join
    or
    array_instance.join(symbol) 

Argument(s) required:

所需参数:

This method takes a symbol and that symbol may be anything that may be used to join the Array instance elements and convert them into a String.

此方法使用一个符号,该符号可以是可用于连接Array实例元素并将其转换为String的任何符号。

Example 1:

范例1:

=begin
  Ruby program to demonstrate JOIN method
=end	

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

puts "Array join implementation."
puts "Enter the Symbol"
sym = gets.chomp

puts lang.join(sym)

puts "Array elements are:"
print lang

Output

输出量

Array join implementation.
Enter the Symbol
 *
C++*Java*Python*Ruby*Perl
Array elements are:
["C++", "Java", "Python", "Ruby", "Perl"]

Explanation:

说明:

In the above code, you can observe that we are trying to create a String with the help of Array instance elements. We are asking the user for the symbol which he wants to put in between the elements of the Array instance.

在上面的代码中,您可以观察到我们正在尝试借助Array实例元素创建一个String。 我们正在询问用户要在Array实例的元素之间插入的符号。

Example 2:

范例2:

=begin
  Ruby program to demonstrate JOIN method
=end	

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

puts "Array join implementation."

puts lang.join

puts "Array elements are:"
print lang

Output

输出量

Array join implementation.
C++JavaPythonRubyPerl
Array elements are:
["C++", "Java", "Python", "Ruby", "Perl"]

Explanation:

说明:

In the above code, you can see that we are using the method without any argument and this has resulted in the normal Array elements joining. All the Array elements are concatenated and converted into a single String.

在上面的代码中,您可以看到我们使用的方法没有任何参数,这导致正常的Array元素加入。 所有Array元素都被串联并转换为单个String。

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

ruby array

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值