The method shown is inject
, (aka reduce
).
I love inject
, and you should too!
Jay Fields loves inject
so much he wrote a love letter to it.
# Before: | |
class Product < ActiveRecord::Base | |
has_many :purchases | |
end | |
class Purchase < ActiveRecord::Base | |
belongs_to :product | |
# We don't want the receipt responsibilities in Purchase | |
def has_receipt? |
2014-03-20 | |
Line 1 | |
Line 2 | |
2014-03-21 | |
Line 1 | |
Line 2 |
## | |
# Helpers for randomized person testing. | |
module PersonsHelper | |
## | |
# Generate the next n elements for the array, but in a predefined manner. | |
class FakeElementGenerator | |
## | |
# Generate elements, the first of which must be start_with. | |
def initialize(start_with:) | |
@start = start_with |
require 'rspec' | |
def sum(numbers) | |
numbers.inject(0) { |sum, number| sum + number } | |
end | |
def product(numbers) | |
numbers.inject(1) { |sum, number| sum * number } | |
end |
Resolving dependencies... | |
cabal: Could not resolve dependencies: | |
trying: yosog-0.0.0 (user goal) | |
rejecting: yosog-0.0.0:!test (global constraint requires opposite flag | |
selection) | |
trying: yosog-0.0.0:*test | |
trying: yesod-test-1.4.3.1 (dependency of yosog-0.0.0:*test) | |
trying: HUnit-1.2.5.2 (dependency of yesod-test-1.4.3.1) | |
trying: yesod-static-1.4.0.4 (dependency of yosog-0.0.0) | |
trying: cryptohash-conduit-0.1.1 (dependency of yesod-static-1.4.0.4) |
The method shown is inject
, (aka reduce
).
I love inject
, and you should too!
Jay Fields loves inject
so much he wrote a love letter to it.
That's a decorator, yo!
I want to read more about decorators.
I want to watch a video where someone teaches me about decorators.
Sure! Symbol-to-proc is what you want here:
users.select(&:admin?)
Note that we couldn't use this if admin?
took an argument, which is kind of a bummer.
commit 81a9375d4ecbd37ca9da7efaebbffa8cf150d6f6 | |
Author: Ben Orenstein <ben.orenstein@gmail.com> | |
Date: Wed Jul 22 12:02:43 2015 -0400 | |
code review ideas | |
diff --git a/app.rb b/app.rb | |
index 93bac6c..c9443f6 100644 | |
--- a/app.rb | |
+++ b/app.rb |