View factory_girl_cache.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FactoryGirl | |
module Strategy | |
class Cache | |
def association(runner) | |
runner.run(:cache) | |
end | |
def result(evaluation) | |
repository.read(evaluation) || repository.store(evaluation) | |
end |
View or.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Or < BasicObject | |
def initialize(a, b) | |
@a = a | |
@b = b | |
end | |
def method_missing(method_name, *arguments, &block) | |
@a.send(method_name, *arguments, &block) || @b.send(method_name, *arguments, &block) | |
end | |
end |
View track_habtm_changes.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See https://groups.google.com/forum/#!topic/rubyonrails-core/Lb9rBkZnZSo | |
module TrackHabtmChanges | |
def self.included(model) | |
model.after_initialize :track_habtm_initial_state | |
model.after_save :track_habtm_initial_state | |
end | |
def changes | |
super.merge(habtm_changes) | |
end |
View active_record.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Robust JSON output for active record errors | |
# | |
# See https://groups.google.com/forum/#!topic/rubyonrails-core/hxgX6D9s2uM | |
module ActiveRecord | |
module AutosaveAssociation | |
def validate_collection_association(reflection) | |
if association = association_instance_get(reflection.name) | |
if records = associated_records_to_validate_or_save(association, new_record?, reflection.options[:autosave]) | |
records.each_with_index do |record, index| | |
association_valid?(reflection, record, index) |
View ruby.vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Add this file to ~/.vim/after/syntax/ruby.vim | |
" | |
" https://thegreata.pe/articles/2018/01/01/vim-syntax-highlighting-for-sql-strings-inside-ruby-code/ | |
unlet b:current_syntax | |
syn include @SQL syntax/sql.vim | |
syn region sqlHeredoc start=/\v\<\<[-~]SQL/ end=/\vSQL/ keepend contains=@SQL | |
let b:current_syntax = "ruby" | |
unlet b:current_syntax |
View OptionalAssignments.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Have the function OptimalAssignments(strArr) read strArr which will represent | |
# an NxN matrix and it will be in the following format: ["(n,n,n...)","(...)",...] | |
# where the n's represent integers. This matrix represents a machine at row i | |
# performing task at column j. The cost for this is matrix[i][j]. Your program | |
# should determine what machine should perform what task so as to minimize the | |
# whole cost and it should return the pairings of machines to tasks in the | |
# following format: (i-j)(...)... Only one machine can perform one task. For | |
# example: if strArr is ["(5,4,2)","(12,4,3)","(3,4,13)"] then your program | |
# should return (1-3)(2-2)(3-1) because assigning the machines to these tasks | |
# gives the least cost. The matrix will range from 2x2 to 6x6, there will be no |
View num_pairs_divisible_by60.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/submissions/ | |
# | |
# @param {Integer[]} time | |
# @return {Integer} | |
def num_pairs_divisible_by60(time) | |
seen = Hash.new(0) | |
total = 0 | |
time.each do |number| | |
number = number % 60 | |
pair = (60 - number) % 60 |
View stable_sort_example.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a array with numbers from 1 to 100 in a random order | |
ary = (1..100).to_a.shuffle + (1..100).to_a.shuffle | |
idx = 0 | |
paired = ary.map.with_index { |value| [value, idx += 1] } | |
# Now the numbers are paired; the first is the random number 1-100 the second is its sequence within the 200 entries | |
puts paired.inspect | |
# You'll see many entries with equal first values where the first of them has a higher second (sequence) number, meaning it's out of order now |
View limit3.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT name, COUNT(*) | |
FROM absences | |
GROUP BY name | |
ORDER BY COUNT(*) DESC | |
LIMIT 3; | |
name | count | |
------------+------- | |
John Doe | 2392 | |
Jane Doe | 1960 |
View limit5.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT name, COUNT(*) | |
FROM absences | |
GROUP BY name | |
ORDER BY COUNT(*) DESC | |
LIMIT 5; | |
name | count | |
------------+------- | |
John Doe | 2392 | |
Jane Doe | 1960 |
OlderNewer