什么是“地图”;方法做在Ruby?

我是编程新手。有人能解释一下.map在:

params = (0...param_count).map
360192 次浏览

它将一个函数“映射”到Enumerable中的每个项——在本例中是一个范围。因此,它将为从0到param_count的每个整数调用传递一次的块(独占-关于点你是对的),并返回一个包含每个返回值的数组。

它也有一个别名,collect

0..param_count表示“直到并包括param_count”。 0...param_count表示“到,但不包括param_count”

Range#map并不返回Enumerable,它实际上将它映射到一个数组。它与Range#to_a相同。

map方法接受一个可枚举对象和一个块,并为每个元素运行块,输出块中的每个返回值(除非使用map!),否则原始对象是不变的:

[1, 2, 3].map { |n| n * n } #=> [1, 4, 9]

ArrayRange是可枚举类型。带有块的map返回Array。map!改变原始数组。

这在哪里有帮助,map!each之间的区别是什么?这里有一个例子:

names = ['danil', 'edmund']


# here we map one array to another, convert each element by some rule
names.map! {|name| name.capitalize } # now names contains ['Danil', 'Edmund']


names.each { |name| puts name + ' is a programmer' } # here we just do something with each element

输出:

Danil is a programmer
Edmund is a programmer

map,以及selecteach是Ruby在我的代码中的主力之一。

它允许您在数组的每个对象上运行一个操作,并将它们全部返回到相同的位置。一个例子是将一个数字数组加1:

[1,2,3].map {|x| x + 1 }
#=> [2,3,4]

如果你可以在数组的元素上运行一个方法,你可以用这样的简写方式来做:

  1. 要用上面的例子做到这一点,你必须这样做

    class Numeric
    def plusone
    self + 1
    end
    end
    [1,2,3].map(&:plusone)
    #=> [2,3,4]
    
  2. To more simply use the ampersand shortcut technique, let's use a different example:

    ["vanessa", "david", "thomas"].map(&:upcase)
    #=> ["VANESSA", "DAVID", "THOMAS"]
    

Transforming data in Ruby often involves a cascade of map operations. Study map & select, they are some of the most useful Ruby methods in the primary library. They're just as important as each.

(map is also an alias for collect. Use whatever works best for you conceptually.)

More helpful information:

If the Enumerable object you're running each or map on contains a set of Enumerable elements (hashes, arrays), you can declare each of those elements inside your block pipes like so:

[["audi", "black", 2008], ["bmw", "red", 2014]].each do |make, color, year|
puts "make: #{make}, color: #{color}, year: #{year}"
end
# Output:
# make: audi, color: black, year: 2008
# make: bmw, color: red, year: 2014

在哈希(也是Enumerable对象)的情况下,哈希只是为解释器提供特殊指令的元组数组。第一个“管道参数”是键,第二个是值。

{:make => "audi", :color => "black", :year => 2008}.each do |k,v|
puts "#{k} is #{v}"
end
#make is audi
#color is black
#year is 2008

要回答这个实际问题:

假设params是一个散列,这将是通过它进行映射的最佳方法:使用两个块参数而不是一个来捕获键&哈希中每个解释元组的值对。

params = {"one" => 1, "two" => 2, "three" => 3}
params.each do |k,v|
puts "#{k}=#{v}"
end
# one=1
# two=2
# three=3

Map是可枚举模块的一部分。和“collect”非常相似。

  Class Car


attr_accessor :name, :model, :year


Def initialize (make, model, year)
@make, @model, @year = make, model, year
end


end


list = []
list << Car.new("Honda", "Accord", 2016)
list << Car.new("Toyota", "Camry", 2015)
list << Car.new("Nissan", "Altima", 2014)


p list.map {|p| p.model}

Map提供了通过块参数返回的数组迭代的值。

使用ruby 2.4,你可以使用transform_values做同样的事情,这个特性从rails提取到ruby。

h = {a: 1, b: 2, c: 3}


h.transform_values { |v| v * 10 }
#=> {a: 10, b: 20, c: 30}

#each

#each为数组中的每个元素运行一个函数。以下两个代码摘录是等价的:

x = 10
["zero", "one", "two"].each{|element|
x++
puts element
}
x = 10
array = ["zero", "one", "two"]


for i in 0..2
x++
puts array[i]
end

#map

#map对数组的每个元素应用一个函数,返回结果数组。以下是等价的:

array = ["zero", "one", "two"]
newArray = array.map{|element| element.capitalize()}
array = ["zero", "one", "two"]


newArray = []
array.each{|element|
newArray << element.capitalize()
}

#map!

#map!类似于#map,但在适当的位置修改数组。以下是等价的:

array = ["zero", "one", "two"]
array.map!{|element| element.capitalize()}
array = ["zero", "one", "two"]
array = array.map{|element| element.capitalize()}