Skip to content

Instantly share code, notes, and snippets.

View 0xradical's full-sized avatar

Thiago Brandão 0xradical

View GitHub Profile
@0xradical
0xradical / docker-machine-use-nfs.sh
Last active October 22, 2015 18:12 — forked from olalonde/docker-machine-use-nfs.sh
Use NFS instead of vboxsf in Docker Machine
#!/bin/bash
#
# This script will mount /Users in the boot2docker VM using NFS (instead of the
# default vboxsf). It's probably not a good idea to run it while there are
# Docker containers running in boot2docker.
#
# Usage: sudo ./boot2docker-use-nfs.sh
#
@0xradical
0xradical / let_spec.rb
Created May 8, 2015 20:35
let_spec.rb
require 'rspec'
class BankAccount
attr_reader :balance
def initialize(initial_value = nil)
@balance = initial_value || 0
end
def deposit(amount)
@balance += amount
end
@0xradical
0xradical / fibers_and_em.rb
Created June 19, 2014 04:36
fibers_and_em.rb
# Trying to grasp fibers and EM!
# Using this example: http://www.slideshare.net/kbal11/ruby-19-fibers (slide 54)
# Fiber#resume(*params) => when first called, it passes all params as block params to
# Fiber.new
# In the next calls of resumes, *params is the return value of any Fiber.yield
# Fiber.yield stops execution of current fiber
# So, in the run-loop from EM, the fiber stops execution and gives up control
@0xradical
0xradical / transaction_debug.rb
Last active August 29, 2015 13:57
Debugging transactions ...
ObjectSpace.each_object(ActiveRecord::ConnectionAdapters::Transaction).map do |a|
[a.object_id, a.class, a]
end
ObjectSpace.each_object(ActiveRecord::ConnectionAdapters::Mysql2Adapter).map do |a|
[a.object_id, a.class, a]
end
ObjectSpace.each_object(ActiveRecord::ConnectionAdapters::Mysql2Adapter).map do |a|
[a.object_id, a.open_transactions]
-- Copyright (c) 2009, Ionut Gabriel Stan. All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without modification,
-- are permitted provided that the following conditions are met:
--
-- * Redistributions of source code must retain the above copyright notice,
-- this list of conditions and the following disclaimer.
--
-- * Redistributions in binary form must reproduce the above copyright notice,
-- this list of conditions and the following disclaimer in the documentation
@0xradical
0xradical / hashify.rb
Created November 11, 2013 17:48
Dot-separated to hash using recursive lambda
hashify = ->(str, memo = []){ str =~ /\./ ? (r = str.split(/\./,2); {r[0] => hashify[r[1],memo << r[0]]}) : {str => [memo << str].join('.')} }
# hashify['a.b.c.d'] =>
# {"a"=>{"b"=>{"c"=>{"d"=>"a.b.c.d"}}}}
@0xradical
0xradical / mysql.rb
Created March 25, 2013 17:08
Mysql functions
class MysqlQuery
def initialize
@query = ""
end
end
@0xradical
0xradical / fizz_buzz.rb
Last active December 15, 2015 02:59
FizzBuzz in Ruby
1.upto(100).each do |number|
if number % 3 != 0 && number % 5 != 0
puts "#{number}: #{number}"
else
puts "#{number}: #{number % 3 == 0 ? 'Fizz' : '' }#{number % 5 == 0 ? 'Buzz' : ''}"
end
end
@0xradical
0xradical / without_nasty_chain.rb
Last active December 14, 2015 19:38
POC - wicked_pdf and remotipart without nasty alias_method_chain
# wicked pdf
module WickedPdfHelper
def render(options = nil, *args, &block)
if options.is_a?(Hash) && options.has_key?(:pdf)
puts 'do pdf stuff'
end
super
end
end
@0xradical
0xradical / test_encoding.rb
Last active December 14, 2015 17:09
Test string against every encoding present in Encoding.aliases.values
test_string = 'Informação'
Encoding.aliases.values.uniq.each do |encoding|
begin
puts "In #{encoding}: #{test_string.clone.force_encoding(encoding)}"
rescue
puts "Barfed for #{encoding}"
end
end