Skip to content

Instantly share code, notes, and snippets.

module Tutorials
def the_best_one_out_there
"PJ Celis' tutorial on getting to know a code base"
end
end
class RecommendationsController < ApplicationController
def tweet_out_the_best
recommended_tutorial = Tutorials.the_best_one_out_there
Tweeter.send(tweet(recommended_tutorial))
FACTUAL_KEY = #omitted
FACTUAL_SECRET_KEY = #omitted
LOB_TEST_API_KEY = #omitted
require 'factual'
require 'lob'
factual = Factual.new(FACTUAL_KEY, FACTUAL_SECRET_KEY)
hotels = factual.table("hotels-us").limit(20).rows
require 'minitest/autorun'
class Dollar
def initialize(amount)
@amount = amount
end
def times(multiplier)
Dollar.new(@amount * multiplier)
end
def equals?(object)
@pjc
pjc / tdd.rb
Created July 24, 2013 14:59
TDD by example chapter 5/6 equality / privacy
require 'minitest/autorun'
# WE NEED TO BE ABLE TO ... AND ... GIVEN ...
# WE NEED TO BE ABLE TO add amounts in different currencies AND convert the result GIVEN a set of exchange rates
# $5 + 10 CHF = $10 if CHF:USD rate is 2:1
# WE NEED TO BE ABLE TO multiply a number (of shares) with an amount (price per share) AND receive an amount
# $5 * 2 = $10
# money rounding, make amount private, equals(), hashCode(), equal null, equal object?
class Dollar
def jobs
Job.where(user_id: self.id)
end
@pjc
pjc / euler10.rb
Created July 3, 2012 00:04
Euler Project in Ruby - Problem 10
x = 2_000_000
def prime? x
(2...x).each { |i| return false if x % i == 0 }
true
end
def primes upto
a = []
(2..upto).each { |i| a << i if prime? i ; puts i }
@pjc
pjc / euler9.rb
Created July 2, 2012 00:58
Euler Project in Ruby - Problem 9
# Euclid's formula yields Pythagorean triples for integers m and n with m < n:
# a = m**2 - n**2 ; b = 2*m*n ; c = m**2 + n**2
x = 1000
def euclids upto
result = []
(2..upto).each do |m| # Start at 2 as 1 results nothing anyway
(1...m).each do |n| # Euclid's formula only works for m > n
result << [m**2 - n**2, 2*m*n, m**2 + n**2]
@pjc
pjc / euler8.rb
Created July 1, 2012 22:46
Euler Project in Ruby - Problem 8
# Ideone.com can't handle the full integer, our integer n is only the relevant line of the 1000 digit number.
# n = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588
@pjc
pjc / euler7.rb
Created July 1, 2012 22:44
Euler Project in Ruby - Problem 7
def prime? x
(2..x-1).each { |y| return false if x % y == 0 }
true
end
x = 10_001 # We look for 10 001st prime number
n = 3 # Start at 3 so we can skip even numbers
counter = 1 # Counter at 1 because 2 is prime number
while true
@pjc
pjc / euler6.rb
Created July 1, 2012 22:43
Euler Project in Ruby - Problem 6
def sum_of_squares upto
a = 0
(1..upto).each { |x| a += x**2 }
a
end
def square_of_sum upto
a = 0
(1..upto).each { |x| a += x }
a**2