Skip to content

Instantly share code, notes, and snippets.

View brainopia's full-sized avatar

Ravil Bayramgalin brainopia

View GitHub Profile
(function() {
if (window.setImmediate) return;
var timeouts = [];
var messageName = "zero-timeout-message";
// Like setTimeout, but only takes a function argument. There's
// no time argument (always zero) and no arguments (you have to
// use a closure).
function setZeroTimeout(fn) {
class Chart
@initAll: ->
Bar.init()
Spark.init()
@init: ->
for element in $(@selector)
do (element) =>
setImmediate =>
unless $(element).data('initialized')
@brainopia
brainopia / gist:2215241
Created March 27, 2012 11:47
Set bom recursively to ruby files
#!/usr/bin/env ruby
require 'epath'
bom = "\xEF\xBB\xBF".force_encoding('binary')
Path.glob('**/*.rb') do |file|
next if file.binread(3) == bom
previous = file.binread
file.write bom + previous
end
#!/bin/sh
rvm_current=`rvm current`
echo "Installing ruby-debug with $rvm_current ..."
curl -OL http://rubyforge.org/frs/download.php/75414/linecache19-0.5.13.gem
curl -OL http://rubyforge.org/frs/download.php/75415/ruby-debug-base19-0.11.26.gem
gem install linecache19-0.5.13.gem ruby-debug-base19-0.11.26.gem ruby-debug19 -- --with-ruby-include=$rvm_path/src/$rvm_current
ActiveRecord::Base.subclasses.
map {|model| model.reflections.values.select {|association| [:has_many, :has_one].include? association.macro }}.flatten.
each {|association| association.options[:dependent] = :destroy }
# heredocs are quite flexible
without_initial_indentation = <<-END.gsub(/^ {4}/, '')
foo
bar
foo
bar
END
# another example
# Get backtrace for each thread
# ruby 1.9
Thread.list.map(&:backtrace)
# ruby enterprise edition
# http://www.rubyenterpriseedition.com/documentation.html#_obtaining_the_backtrace_of_all_threads
require 'thread'
caller_for_all_threads
# call methods like constants
module Foo
def self.bar
'tadam'
end
end
Foo::bar # => tadam
# power of extend
# can be used to add methods only to top-level object
module Rake
def task
:executed
end
end
extend Rake
# "python-like" import
load path, true # => will load new constants and methods under anonymous module
Module.nesting.first # => inside loaded file will return the current namespace
###
module PythonImport
def import(path)
load 'importer.rb', true