array.map_Array.map和Array.map! Ruby中带有示例的方法

array.map

Ruby Array.map和Array.map! 方法 (Ruby Array.map and Array.map! Methods)

In the last article, we have studied about Array.reverse_each method. This method is used to print the Array in reverse order without bringing any changes in the value of elements stored in the Array instance. Array.each also works in the same manner and a type of non-destructive method. But we need a way through which we can bring some changes in the Element values or we can bring some changes in the value present in the Array.

在上一篇文章中,我们研究了Array.reverse_each方法 。 此方法用于以相反的顺序打印Array,而不会更改Array实例中存储的元素的值。 Array.each也以相同的方式和一种非破坏性方法工作。 但是我们需要一种方法,可以对Element值进行一些更改,也可以对Array中存在的值进行一些更改。

Array.map method is meant for this purpose. It is used to generate a new array with the help of the present array with some changes which are supplied in the block. Array.map is a non-destructive method which simply means that it will not affect the actual Array whereas if you want to bring changes in the actual Array as well, you can introduce Array.map! method in your code.

Array.map方法就是为此目的而设计的。 它用于在当前数组的帮助下生成新数组,并在块中提供一些更改。 Array.map是一种非破坏性的方法,仅意味着它不会影响实际的Array,而如果您也想在实际的Array中进行更改,则可以引入Array.map! 代码中的方法

Syntax:

句法:

    Array.map { |var| #statements}
    
    # or
    
    Array.map! { |var| #statements}
    
    # or
    
    Array.map do |var| 
        #statements
    end
    
    # or
    
    Array.map! do |var| 
        #statements
    end

Parameter(s):

参数:

This method does not take any type or any number of parameters.

此方法不使用任何类型或任何数量的参数。

Example 1:

范例1:

=begin
  Ruby program to demonstrate Array.map
=end

# array declaration
Adc = ['Ruby','Includehelp.com','Ruby','C++','C#','Java','Python','C++','Java']

puts Adc.map {|ele|
	ele = ele + " is a language"
}

puts "Actual elements are:"
print Adc

Output

输出量

Ruby is a language
Includehelp.com is a language
Ruby is a language
C++ is a language
C# is a language
Java is a language
Python is a language
C++ is a language
Java is a language
Actual elements are:
["Ruby", "Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++", "Java"]

Explanation:

说明:

In the above code, you can observe that Array.map is not bringing any change in the actual Array element values. We are concatenating String "is a language" with every element present in the Array. The above code can also be modified with the help of do- end block. When we are printing the Array, we are getting the elements with their actual values.

在上面的代码中,您可以观察到Array.map并未对实际Array元素值进行任何更改。 我们将字符串“是一种语言”与数组中存在的每个元素连接在一起。 上面的代码也可以借助doend块进行修改。 当我们打印数组时,我们得到的是元素的实际值。

Example 2:

范例2:

=begin
Ruby program to demonstrate Array.map!
=end

# array declaration
Adc = ['Ruby','Includehelp.com','Ruby','C++','C#','Java','Python','C++','Java']

cnt = 1
puts Adc.map! {|ele|
	ele = ele + " is a language"
}

puts "Actual elements are:"
puts Adc

Output

输出量

Ruby is a language
Includehelp.com is a language
Ruby is a language
C++ is a language
C# is a language
Java is a language
Python is a language
C++ is a language
Java is a language
Actual elements are:
Ruby is a language
Includehelp.com is a language
Ruby is a language
C++ is a language
C# is a language
Java is a language
Python is a language
C++ is a language
Java is a language

Explanation:

说明:

In the above code, you can observe that Array.map! is bringing change in the actual Array elements. This is a very fruitful method when you want to update the actual values of the element present in the Array. Alike Array.map, this method can also be modified with the help of a do-end block.

在上面的代码中,您可以观察到Array.map! 给实际的Array元素带来了变化。 当您要更新数组中存在的元素的实际值时,这是一种非常有效的方法。 与Array.map类似 ,也可以在do-end块的帮助下修改此方法。

翻译自: https://www.includehelp.com/ruby/array-map-and-array-map-methods-with-example.aspx

array.map

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值