select ...as
by Declan Meehan
由Declan Meehan
一起使用.select .map和.reduce方法可充分利用Ruby (Get the most out of Ruby by using the .select .map and .reduce methods together)
You should absolutely never ever repeat yourself when writing code. In other words, do not repeat yourself twice. To be clear — do not write something which has been explained already.
編寫代碼時,絕對絕對不要重復自己。 換句話說,不要重復自己兩次。 要清楚—不要寫已經解釋過的內容。
This is called tautology, and when writing code it should be avoided at all times. For instance, wouldn’t it have been nice instead of reading this lengthy paragraph if I just used the three powerful words “never, repeat, yourself”?
這稱為重言式,在編寫代碼時應始終避免。 例如,如果我只使用三個功能強大的詞“永不重復,自己動手”,那不是閱讀這段冗長的段落會很好嗎?
Well that’s what I’m going to show you how to do with Ruby’s .select .map and .reduce(or .inject) methods.
好的,這就是我將向您展示如何使用Ruby的.select .map和.reduce(或.inject)方法的方法。
例 (Example)
Let’s suppose you are looking at a dictionary full of employee’s names, job titles, and salaries. Let’s also imagine that you wanted to find out the total amount that the company was spending on developers’ salaries. Now, without using a single method in Ruby, you would most likely write your code out something like this:
假設您正在看一本字典,里面有雇員的姓名,職稱和薪水。 我們還假設您想找出公司在開發人員薪金上花費的總額。 現在,無需在Ruby中使用單個方法,您很可能會編寫出如下代碼:
people = [{first_name: "Gary", job_title: "car enthusiast", salary: "14000" }, {first_name: "Claire", job_title: "developer", salary: "15000"}, {first_name: "Clem", job_title: "developer", salary: "12000"}
]
person1 = people[0][:job_title]
person2 = people[1][:job_title]
person3 = people[2][:job_title]
total = 0
if person1 == "developer"total += people[0][:salary].to_i
end
if person2 == "developer"total += people[1][:salary].to_i
end
if person3 == "developer"total += people[2][:salary].to_i
end
puts total
Wow — that is a lot of lines to write to find only three people. Imagine if the company employed hundreds of people!
哇,寫很多行才能找到三個人。 想象一下,如果公司雇用了數百名員工!
Now if you know a bit about loops, then the next easiest step would be to write an each method to put all the salaries together. This might only take up five or six lines but check this out!
現在,如果您對循環有所了解,那么下一個最簡單的步驟將是編寫一個each方法來將所有薪水放在一起。 這可能只占用五或六行,但請檢查一下!
puts people.select{|x| x[:job_title] == "developer"}.map{|y| y[:salary].to_i}.reduce(:+)
You’ll notice every method begins and ends with a curly bracket. This can be used instead of the do and end commands if it is a single line block.
您會注意到每種方法都以大括號開頭和結尾。 如果它是單個行塊,則可以使用它代替do和end命令。
{} == (do end) #for single-line blocks only
。選擇 (.select)
Let’s start with the .select method. We create a variable (x) and iterate over every method in the people array. It then checks with a boolean expression if the key of (:job_title) is equal to the “developer” string. If the boolean returns true, then the select method places the hash that returned true into a new object.
讓我們從.select方法開始。 我們創建一個變量(x)并遍歷people數組中的每個方法。 然后,它使用布爾表達式檢查(:job_title)的鍵是否等于“開發人員”字符串。 如果布爾值返回true,則select方法將返回true的哈希值放入新對象。
。地圖 (.map)
The map method is used for creating a new array that does not affect the array it is looping through. I used this method to create a new variable (y), and then used that variable to grab the value of the key (:salary). Then, finally, I turned that value from a string into an integer.
map方法用于創建一個新數組,該數組不會影響正在循環通過的數組。 我使用此方法創建了一個新變量(y),然后使用該變量來獲取鍵(:salary)的值。 然后,最后,我將該值從字符串轉換為整數。
。減少 (.Reduce)
This one probably looks the most confusing so let's expand it a bit.
這個看起來可能最令人困惑,所以讓我們擴展一下。
.reduce(0){|sum, indv| sum + indv} #is the same as .reduce(:+)
The reduce method creates a new variable which you set the value equal to in the first parentheses (0). You then create two new values (sum and indv) of which one is the sum that you add the individual salaries to.
reduce方法將創建一個新變量,您可以在第一個括號(0)中將該值設置為等于。 然后,您創建兩個新值(sum和indv),其中一個是您將各個薪金相加的總和。
I hope that explains it well! Please let me know if you have any questions.
我希望這能很好地解釋! 請讓我知道,如果你有任何問題。
翻譯自: https://www.freecodecamp.org/news/ruby-using-the-select-map-and-reduce-methods-together-a9b2af30804b/
select ...as