Skip to content

Instantly share code, notes, and snippets.

@grodowski
grodowski / rake_context.rb
Created June 12, 2014 14:14
Testing Rake tasks like a boss
require "rake"
shared_context "rake" do
let(:rake) { Rake::Application.new }
let(:task_name) { self.class.top_level_description }
let(:task_path) { "lib/tasks/#{task_name.split(":").first}" }
subject { rake[task_name] }
def loaded_files_excluding_current_rake_file
$".reject {|file| file == Rails.root.join("#{task_path}.rake").to_s }
@grodowski
grodowski / ffmpeg_mobile
Last active January 13, 2020 09:58
Mobile optimized HTML5 video encoding using FFMPEG
# webm
ffmpeg -y -i snp-inst3.mp4 \
-filter:v scale=640:360,setsar=1/1 -pix_fmt yuv420p \
-vpre libvpx-720p -b:v 500k -r:v 25/1 -force_fps \
-c:a libvorbis -b:a 80k -pass 1 out_inst.webm
# mp4
ffmpeg -y -i snp-inst3.mp4 \
-filter:v scale=640:360,setsar=1/1 -pix_fmt yuv420p \
-c:v libx264 -preset:v slow -profile:v baseline \
@grodowski
grodowski / trace_calls.rb
Last active August 29, 2015 14:26 — forked from toch/trace_calls.rb
Decorate any method in Ruby to get its call stack
def trace_calls_on
scope = {}
trace = TracePoint.new(:call, :line) do |tp|
case tp.event
when :call then puts "#{tp.path}:#{tp.lineno} #{tp.defined_class}::#{tp.method_id} " \
"called from #{scope[:path]}:#{scope[:lineno]} #{scope[:class]}::#{scope[:method_id]}"
when :line then scope = {
event: :line,
lineno: tp.lineno,
path: tp.path,
@grodowski
grodowski / memory_profiling_scripts
Last active September 25, 2015 09:04
Random tools for memory profiling in Ruby
http://blog.skylight.io/hunting-for-leaks-in-ruby/
http://www.be9.io/2015/09/21/memory-leak/
http://samsaffron.com/archive/2015/03/31/debugging-memory-leaks-in-ruby
# Parse a JSON dump from ObjectSpace.dump_all and sort it to show object allocations count (descending)
# parse_dump.sh
cat $0 |
ruby -rjson -ne 'obj = JSON.parse($_).values_at("file","line","type"); puts obj.join(":") if obj.first ' |
sort |
uniq -c |
@grodowski
grodowski / sidekiq.rb
Created September 25, 2015 09:16
Sidekiq process heap dump to a TCP socket
if ENV["PROFILE"]
require "objspace"
ObjectSpace.trace_object_allocations_start
Sidekiq.logger.info "allocations tracing enabled"
module Sidekiq
module Middleware
module Server
class Profiler
# Number of jobs to process before reporting
@grodowski
grodowski / clean-docker.sh
Created October 30, 2015 10:51
Docker Machine cleanup commands
docker rm `docker ps -a | grep Exited | awk '{print $1 }'`
docker rmi `docker images -aq`
@grodowski
grodowski / pipes.rb
Created November 27, 2015 10:03
codequest_pipes idea
# Sample controller
def create
ctx = Pipes::Context.new(…..)
flow.call(ctx)
render json: ctx.project, status: :ok
# no need for handling specific exceptions of multiple types
resuce Pipes::Error => e
render json: {errors: e.message}
end
var Foo = function() {
return 0;
}
function Foo() {
return 1;
}
Foo.prototype.speak = function () {
return 2;
function onExit(astChild: ts.Node, child: types.Node) {
switch (astChild.kind) {
case ts.SyntaxKind.ModuleDeclaration:
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.SourceFile:
this.namespaceStack.pop()
break
// BinaryExpression may contain a prototype function declaration, which
// should be given a NAMESPACE role. It's indicated by the state of
@grodowski
grodowski / source.js
Created November 7, 2016 13:28
JS sample for Medium
function Foo() {
this.bacon = “Bacon!”;
}
Foo.prototype.say = function() {
console.log(this.bacon);
}