Skip to content

Instantly share code, notes, and snippets.

Avatar

Gabriel Sobrinho sobrinho

View GitHub Profile
@sobrinho
sobrinho / factory_girl_cache.rb
Last active Dec 10, 2018
Object caching for factory girl
View factory_girl_cache.rb
module FactoryGirl
module Strategy
class Cache
def association(runner)
runner.run(:cache)
end
def result(evaluation)
repository.read(evaluation) || repository.store(evaluation)
end
@sobrinho
sobrinho / or.rb
Created Oct 3, 2012
Proof of concept for `(a | b).empty?` on ruby
View or.rb
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
@sobrinho
sobrinho / track_habtm_changes.rb
Last active Dec 21, 2015
Track habtm changes on active record
View track_habtm_changes.rb
# 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
# 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)
@sobrinho
sobrinho / ruby.vim
Created Apr 21, 2020
Highlight SQL and GraphQL in Ruby files
View ruby.vim
" 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
# 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
# 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
@sobrinho
sobrinho / stable_sort_example.rb
Last active May 3, 2022 — forked from cyberfox/stable_sort_example.rb
Showing unstable and stable sorting in Ruby.
View stable_sort_example.rb
# 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
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
SELECT name, COUNT(*)
FROM absences
GROUP BY name
ORDER BY COUNT(*) DESC
LIMIT 5;
name | count
------------+-------
John Doe | 2392
Jane Doe | 1960