Skip to content

Instantly share code, notes, and snippets.

View r00k's full-sized avatar
💭
Working on Tuple (https://tuple.app)

Ben Orenstein r00k

💭
Working on Tuple (https://tuple.app)
View GitHub Profile
@r00k
r00k / ex5.rb
Last active August 29, 2015 13:58
Another DIP example
# 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
@r00k
r00k / inject_spec.rb
Created January 9, 2015 17:26
Leveling up our inject skills
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)
@r00k
r00k / a1.md
Last active August 29, 2015 14:17
Ruby Quiz Question 2

Answer to Question 1:

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.

@r00k
r00k / a1.md
Last active August 29, 2015 14:17
Ruby Quiz Question 3

Answer to Question 2:

The syntax shown is for required keyword arguments.

Here's an example:

def foo(bar:)
  puts bar
end
@r00k
r00k / a3.md
Last active August 29, 2015 14:17
Ruby Quiz Question 4
@r00k
r00k / a3.md
Last active August 29, 2015 14:20
Quizzy Question 4

Answer to Question 3

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