Skip to content

Instantly share code, notes, and snippets.

@dolzenko
dolzenko / parse_ini_file_using_enumerable_slice_before.rb
Created March 9, 2010 19:54
parse_ini_file_using_enumerable_slice_before.rb
def parse_ini_file(file)
# Use +Enumerable#slice_before+ to slice ini file into sections where
# start of new section is detected with regexp matching the opening square bracket
file.each_line.slice_before(/\[/).each_with_object({}) do |section, config|
# Remove the first element which is the section header and extract header name
section_name = section.shift[/\[(?<section_name>.+?)\]/, :section_name] # use Oniguruma's named capture
# Use Hash[] class method to create parameters hash from key/value assoc array
section_parameters = Hash[section.map { |parameter| parameter.split("=").map(&:strip) }]
@dolzenko
dolzenko / extract_tables_from_dump_using_enumerable_slice_before.rb
Created March 9, 2010 20:33
extract_tables_from_dump_using_enumerable_slice_before
if ARGV.size < 1
puts "Use like mysqldump database_name | ./#{ $PROGRAM_NAME } table_name[s]"
exit(0)
end
# Dump for each table in standard mysqldump output is started with the line like:
#
# -- Table structure for table `access_rights`
#
# Use +Enumerable#slice_before+ to slice the whole dump into sections per table
@dolzenko
dolzenko / grep_rails_log_file.rb
Created March 10, 2010 11:44
grep_rails_log_file
#!/usr/bin/env ruby
if ARGV.empty?
puts <<-USAGE
Use like tail -f log/production.log | #{ $PROGRAM_NAME } request_regexp
Examples:
Show only local requests:
tail -f log/production.log | #{ $PROGRAM_NAME } 127.0.0.1
@dolzenko
dolzenko / isolate.bat
Created March 20, 2010 08:26
Quietly enjoy your reclusive tendencies
taskkill /f /im gnotify.exe
taskkill /f /im skype.exe
taskkill /f /im miranda32.exe
diff --git a/ext/internal/node/node_type_descrip.c.rpp b/ext/internal/node/node_type_descrip.c.rpp
index 10ae8a3..5ae8ca4 100644
--- a/ext/internal/node/node_type_descrip.c.rpp
+++ b/ext/internal/node/node_type_descrip.c.rpp
@@ -3,7 +3,7 @@
Node_Type_Descrip node_type_descrips_unsorted[] = {
#ruby <<END
-require 'node_type_descrip'
+require File.expand_path('../node_type_descrip', __FILE__)
@dolzenko
dolzenko / includable_with_options.rb
Created April 1, 2010 20:39
include modules passing options
# Check Thomas Sawyer take on the problem http://github.com/rubyworks/paramix
module IncludableWithOptions
class << self
attr_accessor :last_default_options
end
def self.included(includable_with_options)
%w(string/methodize kernel/constant module/basename module/spacename).each { |facets_core_ext| require "facets/#{ facets_core_ext }" }
raise "IncludableWithOptions should be included by the Module" unless includable_with_options.instance_of?(Module)
@dolzenko
dolzenko / try_block.rb
Created April 8, 2010 08:23
Exceptions based guarded method invocation or glorified `rescue nil`
module TryBlock
def self.install!
unless Object.include?(self)
Object.send(:include, self)
Object.send(:public, :try_block)
end
end
def self.is_nil_exception_message?(message)
message =~ /You have a nil object when you didn't expect it/ ||
@dolzenko
dolzenko / io_interceptor.rb
Created April 19, 2010 22:00
Safe way to intercept writes to IO stream
# Safe way to intercept IO stream
# where just replacing STDOUT doesn't work:
# http://rubyforge.org/tracker/index.php?func=detail&aid=5217&group_id=426&atid=1698
#
module IoInterceptor
def intercept
begin
@intercept = true
@intercepted = ""
yield
@dolzenko
dolzenko / shell_out.rb
Created April 20, 2010 00:44
Shell out using pseudo-tty where available
# ## ShellOut
#
# Provides a convenient feature-rich way to "shell out" to external commands.
# Most useful features come from using `PTY` to execute the command. Not available
# on Windows, `Kernel#system` will be used instead.
#
# ## Features
#
# ### Interruption
#
@dolzenko
dolzenko / global_net_http_debug.rb
Created April 20, 2010 15:45
global_net_http_debug.rb
require "net/http"
Net::HTTP.module_eval do
def D(msg)
(@debug_output ||= STDERR) << msg << "\n"
end
end