Skip to content

Instantly share code, notes, and snippets.

@neudabei
neudabei / snippet.rb
Last active Aug 14, 2018
fl_medium_blogpost
View snippet.rb
# ...
{ fields:
subject_categories.map do |sc|
selected = interested_subject_categories.include?(sc)
{ title: sc.title, value: sc.id.to_s, selected: selected }
end
}
@neudabei
neudabei / snippet.rb
Created Aug 10, 2018
fl_medium_blogpost
View snippet.rb
# These two queries
subject_categories = SubjectCategory.in_alphabetical_order
interested_subject_categories = current_user.interested_subject_categories
# will now return the objects set up in our test
let(:subject_category_interests) { [subject_category_one, subject_category_three] }
let(:subject_categories) { [subject_category_one, subject_category_two, subject_category_three] }
View describe_subject_category_interest_props_using_doubles.rb
describe '#subject_category_interest_props' do
let(:current_user) { double(:user) }
let(:subject_category_one) { double(:subject_category_one, id: 1, title: 'subject-category-title-1') }
let(:subject_category_two) { double(:subject_category_two, id: 2, title: 'subject-category-title-2') }
let(:subject_category_three) { double(:subject_category_three, id: 3, title: 'subject-category-title-3') }
let(:subject_category_interests) { [subject_category_one, subject_category_three] }
let(:subject_categories) { [subject_category_one, subject_category_two, subject_category_three] }
View describe_subject_category_interest_props.rb
describe '#subject_category_interest_props' do
## arrange
# create all subject categories
let!(:subject_category_one) { FactoryBot.create(:subject_category, id: 1, title: 'subject-category-title-1') }
let!(:subject_category_two) { FactoryBot.create(:subject_category, id: 2, title: 'subject-category-title-2') }
let!(:subject_category_three) { FactoryBot.create(:subject_category, id: 3, title: 'subject-category-title-3') }
# a learner has some subject category interests
View subject_category_interest_props.rb
def subject_category_interest_props
subject_categories = SubjectCategory.in_alphabetical_order
interested_subject_categories = current_user.interested_subject_categories
{ fields:
subject_categories.map do |sc|
selected = interested_subject_categories.include?(sc)
{ title: sc.title, value: sc.id.to_s, selected: selected }
end }
end
View verify_proof_of_work.rb
require 'digest'
puts Digest::SHA256.hexdigest("hello world!" + "2030350")
@neudabei
neudabei / proof_of_work.rb
Last active Feb 17, 2018
Understanding proof-of-work using Ruby - https://blog.robertsj.com/proof-of-work
View proof_of_work.rb
class ProofOfWork
require 'digest'
def initialize(difficulty:)
@required_number_of_zeros = difficulty
end
def find_nonce_for(block_content:)
nonce = 0
@neudabei
neudabei / action_controller_test.rb
Created Sep 6, 2016
vanity/test/frameworks/rails/action_controller_test.rb
View action_controller_test.rb
require "test_helper"
class UseVanityControllerTest < ActionController::TestCase
tests UseVanityController
def setup
super
metric :sugar_high
new_ab_test :pie_or_cake do
metrics :sugar_high
@neudabei
neudabei / use_vanity_controller.rb
Created Sep 6, 2016
vanity/test/dummy/app/controllers/use_vanity_controller.rb
View use_vanity_controller.rb
class UseVanityController < ActionController::Base
class TestModel
def test_method
Vanity.ab_test(:pie_or_cake)
end
end
attr_accessor :current_user
def index
View sieves_test.rb
require 'minitest/autorun'
require_relative 'sieve_of_eratosthenes'
class SieveTest < Minitest::Test
def test_a_few_primes
expected = [2, 3, 5, 7]
assert_equal expected, Sieve.new(10).primes
end
def test_primes