Skip to content

Instantly share code, notes, and snippets.

View brainopia's full-sized avatar

Ravil Bayramgalin brainopia

View GitHub Profile
#!/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
# magic double assignment
a = [[a,1]] # => [[[[nil, 1]], 1]]
# fun with constant resolution
class Magic
def self.const_missing(const)
'for children'
end
end
p Magic::Bunny # => for children
# Module.constants allows you to check all available at point of call constants
module Bar
AvailableUnderNamespace = :constant
end
class Foo
include Bar
p Module.constants.include?(:AvailableUnderNamespace) # => true
end