select ...as_一起使用.select .map和.reduce方法可充分利用Ruby

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值