Skip to content

Instantly share code, notes, and snippets.

@fabrizioc1
fabrizioc1 / tc_encoding.rb
Created February 28, 2011 20:38
A test case to check how libxml-ruby handles ruby 1.9 string encodings
# encoding: UTF-8
require 'xml'
require 'test/unit'
class EncodingTest < Test::Unit::TestCase
def setup()
@xml = <<-EOS
<?xml version='1.0' encoding='UTF-8'?>
<test>
@fabrizioc1
fabrizioc1 / ruby_extend_and_include.rb
Created March 21, 2011 21:35
The difference between extend and include in Ruby
require 'test/unit'
require 'active_support/all'
class TestExtendAndInclude < Test::Unit::TestCase
module SomethingExtended
mattr_accessor :data_attributes
def data_attribute(name)
self.data_attributes ||= []
@fabrizioc1
fabrizioc1 / gem-uninstall.rb
Created July 10, 2011 21:00
Clean up all gems
#
# Now run gem-uninstall.sh
# The minimum needed for 1.9.2 is rake, rdoc, and minitest, usually they don't deleted
#
gems=`gem list`.split(/\n/).map{|gem| name=gem.gsub(/\s+\(.+?\)/,""); "gem uninstall -I -x -a #{name}"}
File.open("gem-uninstall.sh","w+") do |f| gems.each{|g| f.puts g} end
@fabrizioc1
fabrizioc1 / random_password.rb
Created July 12, 2011 22:38
Generating a random password in one line
# Ruby 1.9.2
password="".tap{|s| 8.times { s << sprintf("%c",Random.new.rand(33..126)) } }
# Another way
password=(33..126).to_a.shuffle.take(8).map(&:chr).join
# Using only safe/printable characters
valid_chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
password = valid_chars.shuffle.take(8).join
@fabrizioc1
fabrizioc1 / install-sqlite.sh
Created July 14, 2011 18:34
Installing sqlite3 in CentOS for use with ruby-sqlite3
# The YUM package is too old for use with ruby-sqlite3, use the autoconf package from www.sqlite.org
cd /opt
wget http://www.sqlite.org/sqlite-autoconf-3070701.tar.gz
tar xvzf sqlite-autoconf-3070701.tar.gz
ln -s /opt/sqlite-autoconf-3070701 /opt/sqlite3
cd /opt/sqlite3
./configure --prefix=/opt/sqlite3
make
make install
# Shared library will be installed in /usr/local/lib.
@fabrizioc1
fabrizioc1 / ShowMemory.java
Created August 30, 2011 13:40
Show max JVM memory
public class ShowMemory
{
public static void main(String args[])
{
long maxBytes = Runtime.getRuntime().maxMemory();
System.out.println("Max memory: " + maxBytes / 1024 / 1024 + "M");
}
}
@fabrizioc1
fabrizioc1 / fork_test.rb
Created August 31, 2011 08:00
Fork Testing
pid = fork do
#Signal.trap('HUP', 'IGNORE') # Don't die upon logout
10.times do
puts "sleeping in child"
sleep 10
end
end
#Process.waitpid(pid)
Process.wait
@fabrizioc1
fabrizioc1 / gemspec
Created September 2, 2011 16:19 — forked from defunkt/gemspec
Quickly create a gemspec.
#!/usr/bin/env ruby
# Usage: gemspec [-s] GEMNAME
#
# Prints a basic gemspec for GEMNAME based on your git-config info.
# If -s is passed, saves it as a GEMNAME.gemspec in the current
# directory. Otherwise prints to standard output.
#
# Once you check this gemspec into your project, releasing a new gem
# is dead simple:
#
@fabrizioc1
fabrizioc1 / hierarchy.rb
Created September 27, 2011 02:42
Pivot child to parent relationship
@child_and_parent = {
:b=>:a,
:c=>:a,
:x=>:a,
:d=>:b,
:e=>:b,
:f=>:c,
:g=>:c,
:h=>:g,
:i=>:h
@fabrizioc1
fabrizioc1 / oracle_timeout_fix.rb
Created September 30, 2011 04:55 — forked from defunkt/connection_fix.rb
Oracle "not connected to ORACLE" fix
# If your workers are inactive for a long period of time, they'll lose
# their Oracle connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/