Implementing the *ect Methods in Terms of Inject

Josh told me today that one

way to see if someone is any good at Ruby is to have them implement

the *ect methods in terms of inject. This isn’t a fool-proof method,

and I’m also not concerned about it’s

screening value

as much as I am simply interested in how to do it. Most importantly,

do I meet Josh’s standards?

(As Nick pointed out,

anyone who’s spent more than 5 minutes functional programming would

laugh at this. I’m a newbie.)

Before I show my code, I’m going to give you the opportunity to go try

it for yourself. Write custom #detect, #select, #reject, and #collect

methods in terms of Enumerable#inject.

Here’s a little RSpec example that will let you automatically verify

your implementation:

And now for my implementation:

How did you do it?

Self-taught Ruby programmers: Are you stuck solving problems by copying code from tutorials and Stack Overflow?

What if you could ditch tutorials and Stack Overflow forever, and solve programming problems on your own?

Learn the 3 most important skills you need to become a successful Ruby developer, absolutely FREE.

(Visited 24 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

1 − one =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>