Rspec2 - def my_inject(&blk)

From the solution, how does Ruby know by (&blk) we are referring the actual inject method? { |sum, num| sum + num } Thanks!

def my_inject(&blk)
val = self.first
self.drop(1).my_each { |el| val =, el) }

Hi Christine,

That’s what the (&blk) in def my_inject(&blk) does for you! When you have that in your method definition, you tell Ruby to expect the block, i.e. something like { |sum, num| sum + num }. If given a block, Ruby then transforms this into a thing called a “proc”, which is an object that has the .call method defined on it. It’s this method that you’re using in self.drop(1).my_each { |el| val =, el) }.

Does that help?

Very helpful, thanks Matthias!