← Back to Upcase

Inject: Ruby's Fold

(Upcase ) #1
In this episode, Ben and Joe tackle a highly useful but lesser known method from Ruby's Enumerable module: inject. While watching, you'll see how inject is just an abstraction over certain types of recursion. We'll use examples to demonstrate inj...
This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/inject-ruby-s-fold

(Acandael Acandael) #2

I have a question about the first example in the video on Inject:

def sum(array)
  element, *rest = array

so I understand that *rest is taking all the elements of array except the first one, but what is ‘rest’, is it a new array or is it a list?

Thanks for the explanation,


(Pierre Lempérière) #3

Hey Anthony,
rest is an array.
Not sure we use the list terminology in Ruby.
I played around with the code below to grasp what is going on.
Does it help ?

def sum(array)
	element, *rest = array
	case rest
	when []
		puts "Array per recursion: #{rest.inspect}"
		element + sum(rest)

result = sum([1,87,3,4,-5,-48])
puts "Result : #{result}"

(Acandael Acandael) #4

yeah, I tried out the program in irb:

Array per recursion: [87, 3, 4, -5, -48]
Array per recursion: [3, 4, -5, -48]
Array per recursion: [4, -5, -48]
Array per recursion: [-5, -48]
Array per recursion: [-48]
=> 42 

it works :smile:

In 'The Well-Grounded Rubyist" I read something about a 'bare list", hence my question.



(Geoff Harcourt) #5

When I work with less-experienced developers, I find that using #inject is usually a technique that not only are they not using, but haven’t even considered. +1 for promoting it!

(Scott Hollinshead) #6

I had a hard time learning the #inject method, I started this post a while ago when trying to figure it out, there is some useful information on there, that helped me a lot and also a video demonstraiting how it works.

(Jacqueline S. Homan) #7

I would really like to see a practical implementation of this in an app tutorial sometime :smile:

(Ben Orenstein) #8

Joe is actually working on creating an exercise with a number of practical examples. It should be hitting your dashboard within a week or so.

(Scott Hollinshead) #9

I will be looking forward to that.

(Noah Hendrix) #10

Just used reduce to refactor some code I saw on a blog post.

def filter(filtering_params)
  results = self
  filtering_params.each do |key, value|
    results = results.public_send(key, value) if value.present?


def filter(filtering_params)
  filtering_params.reduce(self) do |relation, (scope_name, value)|
    relation.public_send(scope_name, value) if value.present?

I thought others might interested in seeing some random ruby code refactored to use reduce. Also notice how it works nicely with the builder pattern.