Can someone explain a real-world, plain-language use for inject in Ruby?

五迷三道 提交于 2019-12-01 05:35:12

This short video (1 minute) is the best explanation of inject() I have encountered for a newbie.

This blog post has some inject() examples.

Here are a couple of inject() examples in action:

[1, 2, 3, 4].inject(0) {|memo, num| memo += num; memo} # sums all elements in array

The example iterates over every element of the [1, 2, 3, 4] array and adds the elements to the memo variable (memo is commonly used as the block variable name). This example explicitly returns memo after every iteration, but the return can also be implicit.

[1, 2, 3, 4].inject(0) {|memo, num| memo += num} # also works

inject() is conceptually similar to the following explicit code:

result = 0
[1, 2, 3, 4].each {|num| result += num}
result # result is now 10

inject() is also useful to create arrays and hashes. Here is how to use inject() to convert [['dogs', 4], ['cats', 3], ['dogs', 7]] to {'dogs' => 11, 'cats' => 3}.

[['dogs', 4], ['cats', 3], ['dogs', 7]].inject({'dogs' => 0, 'cats' => 0}) do |memo, (animal, num)|
  memo[animal] = num
  memo
end

Here is a more generalized and elegant solution:

[['dogs', 4], ['cats', 3], ['dogs', 7]].inject(Hash.new(0)) do |memo, (animal, num)|
  memo[animal] = num
  memo
end

Again, inject() is conceptually similar to the following code:

result = Hash.new(0)
[['dogs', 4], ['cats', 3], ['dogs', 7]].each do |animal, num|
  result[animal] = num
end
result # now equals {'dogs' => 11, 'cats' => 3}
bta

inject can sometimes be better understood by its "other" name, reduce. It's a function that operates on an Enumerable (iterating through it once) and returns a single value.

There are many interesting ways that it can be used, especially when chained with other Enumerable methods, such as map. Often times, it can be a more concise and expressive way of doing something, even if there is another way to do it.

An example like this may seem useless at first:

range.inject {|sum, x| sum += x}

The variable range, however, doesn't have to be a simple explicit range. It could be (for example) a list of values returned from your database. If you ran a database query that returned a list of prices in a shopping cart, you could use .inject to sum them all and get a total.

In the simple case, you can do this in the SQL query itself. In a more difficult case, such as where some items have tax added to them and some don't, something like inject can be more useful:

cart_total = prices.inject {|sum, x| sum += price_with_tax(x)}

This sort of thing is also particularly useful when the objects in the Enumerable are complex classes that require more detailed processing than a simple numerical value would need, or when the Enumerable contains objects of different types that need to be converted into a common type before processing. Since inject takes a block, you can make the functionality here as complex as you need it to be.

Instead of a range, imagine you've got a list of sales prices for some item on eBay and you want to know the average price. You can do that by injecting + and then dividing by the length.

ActiveRecord scopes are a typical case. If we call scoped on a model, we get an object on which we can chain additional scopes. This lets us use inject to build up a search scope from, say, a params hash:

search_params = params.slice("first_name","last_name","city","zip").
  reject {|k,v| v.blank?}

search_scope = search_params.inject(User.scoped) do |memo, (k,v)|
  case k
  when "first_name"
    memo.first_name(v)
  when "last_name"
    memo.last_name(v)
  when "city"
    memo.city(v)
  when "zip"
    memo.zip(v)
  else
    memo
  end

(Note: if NO params are supplied, this brings back the whole table, which might not be what you wanted.)

My favorite explanation for inject or it's synonym reduce is:

reduce takes in an array and reduces it to a single value. It does this by iterating through a list, keeping and transforming a running total along the way.

I found it in a wonderful article at http://railspikes.com/2008/8/11/understanding-map-and-reduce

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!