Skip to content

Instantly share code, notes, and snippets.

View schneems's full-sized avatar

Richard Schneeman schneems

View GitHub Profile
@schneems
schneems / command-line-only-objectspace-trace.sh
Last active December 19, 2022 13:54
trace where Ruby objects come from with this handy bash 3 liner
echo 'require "objspace"; ObjectSpace.trace_object_allocations_start; Kernel.send(:define_method, :sup) do |obj| ; puts "#{ ObjectSpace.allocation_sourcefile(obj) }:#{ ObjectSpace.allocation_sourceline(obj) }"; end' > tmp/tmp-gemfile
cat Gemfile >> tmp/tmp-gemfile
cat tmp/tmp-gemfile > Gemfile
# $ bundle exec irb
# irb(main):001:0> require 'rails'
# => true
# irb(main):002:0> sup(Rails)
# /Users/richardschneeman/.gem/ruby/2.4.0/gems/railties-5.0.0.1/lib/rails/initializable.rb:3
class Add
attr_reader :result, :is_negative
def initialize(a, b)
@result = a + b
@is_negative = @result < 0
end
end
def add(a,b)
class PriorityQueue
attr_reader :elements
def initialize
@elements = []
end
def <<(element)
@schneems
schneems / asset_sync_is_the_devil.md
Last active June 19, 2021 00:12
I hate asset_sync

A not politically correct assertion of my feelings towards a piece of software:

Note: Repetition builds cynicism, asset_sync isn't bad, but when an asset problem cannot be solved via support it gets escalated to me. Often times someone using asset_sync the problem is due to their use of the library and not from Heroku.

Backstory

The asset sync gem uploads your assets (images, css, javascript) to S3. From there you can either point browsers to the copy on S3 or use a CDN + the S3 bucket. It's a good idea, and solved a problem at one time.

It is no longer needed and you should now use https://devcenter.heroku.com/articles/using-amazon-cloudfront-cdn instead. So rather than copying your assets over to S3 after they are precompiled the CDN grabs them from your website instead. Here's some reasons why it's better.

@schneems
schneems / gist:1705468
Created January 30, 2012 17:09
Receive GZIP params in Rails
def handle_gzip_params
# When the server receives a request with content-type "gzip/json" this will be called which will unzip it,
# and then parse it as json
# The use case is so clients such as Android or Iphone can zip their long request such as Inviters#addressbook emails
# Then the server can unpack the request and parse the parameters as normal.
if request.content_type == "gzip/json"
data = ActiveSupport::JSON.decode(ActiveSupport::Gzip.decompress(request.raw_post))
data = {:_json => data} unless data.is_a?(Hash)
params ||= {}
~ $ gem install mysql2
Fetching mysql2-0.5.3.gem
Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
	ERROR: Failed to build gem native extension.

    current directory: /app/vendor/ruby-2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
/app/vendor/ruby-2.6.5/bin/ruby -I /app/vendor/ruby-2.6.5/lib/ruby/2.6.0 -r ./siteconf20200417-12-1lo3zcx.rb extconf.rb
checking for rb_absint_size()... yes
$ time curl -L --fail --retry 5 --retry-delay 1 --connect-timeout 3 --max-time 30 -I https://s3-external-1.amazonaws.com/heroku-buildpack-ruby/heroku-18/ruby-2.4.0.tgz
# => real 0m0.117s
$ cat <<EOF | ruby
  require 'net/http'
  require 'benchmark'
@schneems
schneems / gist_snippits.rb
Created October 22, 2012 17:10
gist_snippits.rb
require 'json'
## What
# This file takes in a markdown file and converts any [codeblocks](http://daringfireball.net/projects/markdown/syntax#precode)
# into gists, then embeds the gists via JS.
# Watch out for Github rate limiting, via IP, this is not an authenticated request
$ docker run -it --rm ruby:2.6 bash
root@22e145f8dfa6:/# gem install rails
Fetching concurrent-ruby-1.1.5.gem
Fetching thread_safe-0.3.6.gem
Fetching activesupport-5.2.3.gem
Fetching crass-1.0.4.gem
Fetching i18n-1.6.0.gem
Fetching tzinfo-1.2.5.gem
Fetching rack-2.0.7.gem
Fetching rack-test-1.1.0.gem
# frozen_string_literal: true
require "active_record"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.string :name, :email, :username, :password, :birthday, :foo, :bar, :baz, :dog_name
t.timestamps null: false