Skip to content

Instantly share code, notes, and snippets.

View mctaylorpants's full-sized avatar

Alex Taylor mctaylorpants

View GitHub Profile
@mctaylorpants
mctaylorpants / another_required_file.rb
Created January 16, 2016 00:06
Fun with Ruby's BEGIN and END blocks
puts "[3] Inside 'another_required_file.rb'"
BEGIN {
puts "[2] BEGIN inside 'another_required_file.rb'"
}
# END blocks will execute at the end of the program, in reverse order to when
# they're encountered
END {
puts "[5] END inside 'another_required_file.rb'"
@mctaylorpants
mctaylorpants / advanced_queries.md
Created January 19, 2016 19:47
Advanced Active Record querying

We have Users who have many Posts. Posts have many Tags through an associated table we've called Taggings.

We want to get a unique list of all the tags that a given user's Posts have been associated with. So we have to find a user, get all of their post IDs, use those IDs to select the relevant taggings, and finally return a distinct list of related tags.

Handwritten SQL

SELECT DISTINCT "tags".name
FROM  taggings
# run me with `ruby test_inconsistent_active_record_conversion_of_time_into_date.rb`
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
@mctaylorpants
mctaylorpants / test_case_typecasting_of_date.rb
Created June 10, 2016 17:52
Test Case - Inconsistent Typecasting of Time into Date
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# Activate the gem you are reporting the issue against.
@mctaylorpants
mctaylorpants / 99_bottles_example.rb
Created December 8, 2016 17:30
99 Bottles Solution 1
class Bottles
def song
verses(99, 0)
end
def verses(*verses)
output = (verses[1]..verses[0]).to_a.reverse.map { |n| verse(n) }
output.join("\n")
end
### browse commit history using a regex
`git log --reverse -p -G<search term> Gemfile`
Ex: `git log --reverse -p -Gaudited Gemfile`
@mctaylorpants
mctaylorpants / spec_output.sh
Created April 20, 2017 23:44
bundler local spec failure
1) Bundler.setup load order orders the load path correctly when there are dependencies
Failure/Error:
expect(load_path).to start_with(
"/gems/rails-2.3.2/lib",
"/gems/activeresource-2.3.2/lib",
"/gems/activerecord-2.3.2/lib",
"/gems/actionpack-2.3.2/lib",
"/gems/actionmailer-2.3.2/lib",
"/gems/activesupport-2.3.2/lib",
"/gems/rake-10.0.2/lib"
one_time_pad = SecureRandom.random_bytes(AUTHENTICITY_TOKEN_LENGTH)
encrypted_csrf_token = xor_byte_strings(one_time_pad, raw_token)
masked_token = one_time_pad + encrypted_csrf_token
Base64.strict_encode64(masked_token)
@mctaylorpants
mctaylorpants / index.html
Created July 31, 2017 00:09
rails csrf - example index.html
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="vtaJFQ38doX0b7wQpp0G3H7aUk9HZQni3jHET4yS8nSJRt85Tr6oH7nroQc01dM+C/dlDwt5xPff5LwyZcggeg==" />
# actionpack/lib/action_controller/metal/request_forgery_protection.rb
def verify_authenticity_token # :doc:
  # ...
if !verified_request?
  # handle errors ...
  end
end
# ...