Skip to content

Instantly share code, notes, and snippets.

@kylev
kylev / signals_badly.rb
Last active August 29, 2015 14:26
An exploration of common pitfalls in Ruby Exception/Signal handling.
#!/usr/bin/env ruby
# Some members of ruby community continue to misunderstand the interplay of
# signal handlers and exception handling. This is a brief exploration
# via examples. You can run them by uncommenting the invocations at
# the bottom. You can see their behavior with Ctrl-C.
#
# Some of the examples will require a kill -9 from another terminal to
# stop.
#
@leemour
leemour / Devise HAML views
Created May 26, 2013 12:43
Convert generated Devise ERB views to HAML
rails g devise:views
gem install html2haml
for file in app/views/devise/**/*.erb; do html2haml -e $file ${file%erb}haml && rm $file; done
gem install haml2slim
for file in app/views/devise/**/*.haml; do haml2slim $file ${file%haml}slim && rm $file; done
@ozeron
ozeron / dev_profiler.rb
Last active July 9, 2018 00:16
Profiling ruby code
# https://github.com/ruby-prof/ruby-prof
require 'ruby-prof'
class DevProfiler
BASE_PATH = "#{Rails.root}/tmp/performance/#{file_name}"
def self.prof(file_name)
RubyProf.start
puts "Start profiling method"
yield
puts "Done Profiling!"
@yuki24
yuki24 / json_with_synbolize_names.rb
Created January 13, 2017 22:51
JSON with the symbolize_name: true option
require 'json'
require 'open-uri'
require 'benchmark/ips'
require 'memory_profiler'
json = open('https://network.pivotal.io/api/v2/products').read
Benchmark.ips do |x|
x.report('symbolize_names: false') { JSON.parse(json) }
x.report('symbolize_names: true') { JSON.parse(json, symbolize_names: true) }
require 'rails_event_store'
require 'aggregate_root'
PaymentAuthorized = Class.new(RailsEventStore::Event)
PaymentSuccessed = Class.new(RailsEventStore::Event)
PaymentFailed = Class.new(RailsEventStore::Event)
PaymentCaptured = Class.new(RailsEventStore::Event)
class Payment
InvalidOperation = Class.new(StandardError)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Todo;
class TodosController extends Controller
{
@kyledrake
kyledrake / gist:3077989
Created July 9, 2012 18:14
Ruby code for doing P12 to PEM conversion via command line. Supports MRI/JRuby/Rubinius
require 'tempfile'
require 'openssl'
require 'escape' # gem install escape
class CommandFailError < StandardError; end
def p12_to_pem_text(p12, pass='')
pass = '' if pass.nil?
# Use shell command for JRuby (see https://github.com/jruby/jruby-ossl/issues/8)
@emachnic
emachnic / default.rb
Created December 1, 2011 15:29
Chef recipe to write redis.yml on Engine Yard Cloud
# ey-cloud-recipes/cookbooks/redis-yml/recipes/default.rb
if ['app_master', 'app'].include?(node[:instance_role])
redis_instance = node['utility_instances'].find { |instance| instance['name'] == 'redis' }
if redis_instance
node[:applications].each do |app, data|
template "/data/#{app}/shared/config/redis.yml"do
source 'redis.yml.erb'
owner node[:owner_name]

Fibur

Fibur is a library that allows concurrency during Ruby I/O operations without needing to make use of callback systems. Traditionally in Ruby, to achieve concurrency during blocking I/O operations, programmers would make use of Fibers and callbacks. Fibur eliminates the need for wrapping your I/O calls with Fibers and a callback. It allows you to write your blocking I/O calls the way you normally would, and still have concurrent execution during those I/O calls.

Example

Say you have a method that fetches data from a network resource:

@zonomasa
zonomasa / how_to_install_jemalloc_on_macosx
Last active June 14, 2021 22:57
Install jemalloc on Mac OSX
$ git clone git@github.com:jemalloc/jemalloc.git
$ autoconf
$ ./configure
$ make
$ make install