Skip to content

Instantly share code, notes, and snippets.

View awesome's full-sized avatar

So Awesome Man awesome

View GitHub Profile
@olivierlacan
olivierlacan / database.rake
Last active June 10, 2021 07:06
Database rake tasks that I use on Code School to ferret out huge tables with millions of rows and see how many indices they have and to see which tables have missing indices on associated tables (foreign keys). The latter was taken from this great post by Tom Ward: https://tomafro.net/2009/09/quickly-list-missing-foreign-key-indexes
namespace :database do
task fat_tables: :environment do
c = ActiveRecord::Base.connection
max_table_name_width = 0
tables = c.tables.sort_by do |t|
max_table_name_width = t.length if t.length > max_table_name_width
@cmar
cmar / gist:8e947a78470b82899d32
Last active June 28, 2016 16:05
benchmark each_with_object
# each_with_index, each_with_object and inject({}) are all %50 slower then #each
# simple each with an addition noop
[4] pry(main)> puts Benchmark.measure { (1..10_000_000).each { |i| i + 1 } }
0.450000 0.000000 0.450000 ( 0.448808)
# each_with_index
[5] pry(main)> puts Benchmark.measure { (1..10_000_000).each_with_index { |i, index| i + 1 } }
0.650000 0.000000 0.650000 ( 0.645812)
@jcward
jcward / print_amf.rb
Last active June 28, 2017 04:32
RocketAMF can de/serialize AMF serialized byte streams, but it throws (at least, the 0.2.1 version from gem install throws) when it encounters an IExternalizable type in the byte steam. This patch allows it to parsed custom IExternalizable classes properly.
#!/usr/bin/env ruby
require 'rocketamf'
require 'stringio'
# Monkey patch for RocketAMF (0.2.1 gem) that handles IExrternalizable types
# in the input, storing their type as "__as3_type" parameter:
module RocketAMF
module Values #:nodoc:
class TypedHash
@echo Off
set config=%1
if "%config%" == "" (
set config=Release
)
set version=1.0.0
if not "%PackageVersion%" == "" (
set version=%PackageVersion%
)
@moertel
moertel / suppress_ruby_output.rb
Last active March 21, 2024 08:54
Temporarily suppress STDOUT and STDERR (ruby)
# Temporarily redirects STDOUT and STDERR to /dev/null
# but does print exceptions should there occur any.
# Call as:
# suppress_output { puts 'never printed' }
#
def suppress_output
original_stderr = $stderr.clone
original_stdout = $stdout.clone
$stderr.reopen(File.new('/dev/null', 'w'))
$stdout.reopen(File.new('/dev/null', 'w'))
@agendor
agendor / hapijs-rest-api-tutorial.md
Last active August 31, 2021 08:31
A practical introduction to building a RESTful API with the hapi.js server framework for Node.js
@edelpero
edelpero / heroku_rails_phantomjs.md
Last active February 14, 2025 20:11
Heroku, Ruby on Rails and PhantomJS

#Heroku, Ruby on Rails and PhantomJS

In this post, I’m going to show you how to modify an existing Ruby on Rails app running on Heroku’s Cedar stack to use PhantomJS for screen scraping. If you’ve never heard of PhantomJS, it’s a command-line WebKit-based browser (that supports JavaScript, cookies, etc.).

Let’s get started. This is a high-level overview of the required steps:

  • Modify your app to use multiple Heroku buildpacks.
  • Extend your app to use both the Ruby as well as the PhantomJS buildpacks.
  • Confirm that everything worked.
@tacoplusplus
tacoplusplus / keybase.md
Created January 28, 2014 19:42
keybase proof

Verifying myself: I am https://keybase.io/test_851261

As part of this verification process, I am signing this object and posting as a gist as github user tacoplusplus

{
    "body": {
        "key": {
            "fingerprint": "a3a9e2afba2c7edeea6d2552a603027ff4523ba5",
            "host": "keybase.io",
@mjgiarlo
mjgiarlo / benchmark_dead_horse.rb
Last active June 28, 2016 16:04
Ruby inject vs. tap vs. each_with_object. Inspired by http://phrogz.net/tap-vs-each_with_object
require 'benchmark'
N = 1000000
nums = N.times.map{ rand(N) }
enum = [1, 2, 'string', {}, [], false, true, nil]
def process(v)
v
end
# http://37signals.com/svn/posts/3176-three-quick-rails-console-tips
>> helper.truncate("Testing", length: 4)
=> "T..."
>> helper.link_to "Home", app.root_path
=> "<a href=\"/\">Home</a>"