Skip to content

Instantly share code, notes, and snippets.

View bernEsp's full-sized avatar

Bernardo Espinoza bernEsp

View GitHub Profile
require 'benchmark/ips'
env = {"foo" => "bar", "bar" => "baz", "baz" => "boo", "PATH_INFO" => "foo"}
Benchmark.ips do |x|
x.report("set and reset") do
env["PATH_INFO"] = "zoo"
env
env["PATH_INFO"] = "foo"
end
@wycats
wycats / app.js
Last active February 11, 2016 16:08
App.Router.map(function() {
this.resource('post', { path: '/posts/:post_id' });
});
App.PostRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('post', params.post_id);
}
});
@awesome
awesome / ruby-multi-line-string-without-newlines-AND-ruby-multi-line-string-without-concatenation.rb
Created November 21, 2013 15:47
Ruby Multi-line String without Newlines —AND— Ruby Multi-line String without Concatenation
##
# by SoAwesomeMan
str =<<-EOS.gsub(/^[\s\t]*|[\s\t]*\n/, '') # no space "\s" for new line "\n"; kill tabs too
select awesome, awesome, awesome, awesome, awesome, awesome,
from rad, rad, rad, rad, rad, rad, rad, rad, rad, rad, rad,
where cool cool cool cool cool cool cool cool cool cool cool'
EOS
# => "select awesome, awesome, awesome, awesome, awesome, awesome,from rad, rad, rad, rad, rad, rad, rad, rad, rad, rad, rad,where cool cool cool cool cool cool cool cool cool cool cool'"
str =<<-EOS.gsub(/^[\s\t]*/, '').gsub(/[\s\t]*\n/, ' ').strip # yes space "\s" for new line "\n"; kill tabs too
@jjb
jjb / gist:7389552
Last active March 16, 2024 18:48
Ruby 2.1 memory configuration

This all applies to Ruby 2.1. In some cases a setting is not available in 2.0, this is noted. There is also a different with 1.9, 1.8, and REE --- these are not noted.

All the relevant code is in https://github.com/ruby/ruby/blob/master/gc.c

RUBY_HEAP_MIN_SLOTS

default: 10000

The number of heap slots to start out with. This should be set high enough so that your app has enough or almost enough memory after loading so that it doesn't have to allocate more memory on the first request (althogh this probably isn't such a big deal for most apps).

(todo: figure out how big a slot is. i think the answer can be infered from this code.)

@DanielKehoe
DanielKehoe / tutorials.railsapps.org.js
Created January 11, 2013 20:32
JavaScript used to track events on the https://tutorials.railsapps.org site and funnel to segment.io.
// https://segment.io/railsapps/snippet/
var analytics=analytics||[];analytics.load=function(e){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=("https:"===document.location.protocol?"https://":"http://")+"d2dq2ahtl5zl1z.cloudfront.net/analytics.js/v1/"+e+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);var r=function(e){return function(){analytics.push([e].concat(Array.prototype.slice.call(arguments,0)))}},i=["identify","track","pageview","ab"];for(var s=0;s<i.length;s++)analytics[i[s]]=r(i[s])},
analytics.load("xxxxxxxxxx");
// run analytics on every page
$(document).ready(function() {
var user_id = $('#body').data('user_id')
var user_name = $('#body').data('user_name')
var user_plan = $('#body').data('user_plan')
// https://segment.io/railsapps/setup
analytics.identify(user_id, {
@tordans
tordans / script-console-output-with-log.rb
Created November 15, 2011 18:23
.count vs. .size vs. .lenght
>> p = Project.find_by_id(1114)
# Project Load (1) (0.001019) SELECT * FROM `projects` WHERE (projects.deleted_at IS NULL OR projects.deleted_at > '2011-11-15 18:12:54') ORDER BY projects.id DESC LIMIT 1
=> #<Project id: 1114 …>
>> p.pictures.count
# Always a sql-query
# SQL (1) (0.000572) SELECT count(*) AS count_all FROM `basic_pictures` WHERE (`basic_pictures`.owner_id = 8004 AND `basic_pictures`.owner_type = 'Project') AND ( (`basic_pictures`.`type` = 'ProjectPicture' ) )
=> 60
>> p.pictures.size
@scottwb
scottwb / enum_models.rb
Created August 23, 2010 20:37
Enumerate all Rails ActiveRecord::Base model classes.
# Add AR::Base class methods for enumerating all the model
# classes in a Rails project.
module ActiveRecord
class Base
def self.all_model_classes
connection.tables.map do |table|
begin
klass = Class.const_get(table.classify)
klass.ancestors.include?(self) ? klass : nil