Skip to content

Instantly share code, notes, and snippets.

@fabrizioc1
fabrizioc1 / base_converter.rb
Created September 30, 2011 19:35
Number base conversion
# Converts from base 10 to a different base
def get_digit(digit)
digit.to_i > 9 ? sprintf("%0X",digit.to_i) : digit.to_s
end
def to_base(input,new_base)
raise "Number base should be between 2 and 16!" if ((new_base > 16) || (new_base < 2))
raise "Input number must be an unsigned integer!" if (input < 0)
return 0 if input == 0
@fabrizioc1
fabrizioc1 / sanitize.rb
Created October 6, 2011 05:53
Sanitizing UTF8 or ISO8859-1 strings in Ruby 1.8.7
require 'cgi'
require 'active_support/all'
WHITELIST_CHAR_FRENCH_UTF8 =
["\xC3\x80", "\xC3\x84", "\xC3\x88", "\xC3\x89", "\xC3\x8A", "\xC3\x8B", "\xC3\x8E", "\xC3\x8F", "\xC3\x94", "\xC3\x99", "\xC3\x9B",
"\xC3\x9C", "\xC3\x87", "\xC3\xA0", "\xC3\xA2", "\xC3\xA4", "\xC3\xA8", "\xC3\xA9", "\xC3\xAA", "\xC3\xAB", "\xC3\xAE", "\xC3\xAF",
"\xC3\xB4", "\xC3\xB9", "\xC3\xBB", "\xC3\xBC", "\xC3\xBF", "\xC3\xA7"].join
WHITELIST_CHAR_FRENCH_ISO8859_1 =
"\300\304\310\311\312\313\316\317\324\331\333\334\307\340\342\344\350\351\352\353\356\357\364\371\373\374\377\347"+
@fabrizioc1
fabrizioc1 / rack_mini_server.rb
Created October 11, 2011 19:49
Create a self contained Rack server for use with unit tests
module Rack::MiniServer
def start
@server_pid = fork do
puts "start forking ..."
Rack::Handler::WEBrick.run(
Rack::ShowExceptions.new(Rack::Lint.new(self.new)), :Port => 9292) do |server|
['INT', 'TERM'].each{ |signal| trap(signal){ puts "shutting down"; server.shutdown} }
end
end
@fabrizioc1
fabrizioc1 / trigger_javadump.sh
Created November 9, 2011 01:16
Script to trigger java dump when WebSphere JVM does a full GC
#!/bin/bash
#
# Run this in the background and keep it running when you log out run it like this:
# nohup /tmp/trigger_javadump.sh >/tmp/trigger_javadump.log 2>&1 &
#
FULLGC_PATTERN='^Application time: 0.0000'
VERBOSEGC_PATH='verbosegc.log'
while true; do
FULLGC_FOUND=$(grep "$FULLGC_PATTERN" "$VERBOSEGC_PATH")
@fabrizioc1
fabrizioc1 / puzzle_1.rb
Created November 18, 2011 04:51
puzzle 1
one_million = (1..1e6).to_a
one_million_sum = (1.0e6+1)*1.0e6/2
missing = one_million.delete(rand(one_million.size))
one_million_minus_missing_sum = one_million.reduce(0){|sum,current| sum + current}
puts (one_million_sum - one_million_minus_missing_sum) == missing ? "It works" : "Did not work"
@fabrizioc1
fabrizioc1 / fix_encoding.rb
Created November 21, 2011 17:41
Fixing invalid UTF-8 characters
# first method
def enforce_utf8(from = nil)
begin
self.is_utf8? ? self : Iconv.iconv('utf8', from, self).first
rescue
converter = Iconv.new('UTF-8//IGNORE//TRANSLIT', 'ASCII//IGNORE//TRANSLIT')
converter.iconv(self).unpack('U*').select{ |cp| cp < 127 }.pack('U*')
end
end
@fabrizioc1
fabrizioc1 / akamai_debug_headers.txt
Last active May 24, 2023 08:23
Akamai debug headers
Pragma: akamai-x-cache-on, akamai-x-cache-remote-on, akamai-x-check-cacheable, akamai-x-get-cache-key, akamai-x-get-extracted-values, akamai-x-get-nonces, akamai-x-get-ssl-client-session-id, akamai-x-get-true-cache-key, akamai-x-serial-no
@fabrizioc1
fabrizioc1 / invalidation.feature
Created January 13, 2012 00:47
CTO cache invalidation
@MQC9056 @inventory @invalidation
Feature: Inventory Cache Invalidation
As a SonyStyle user
In order to place an order for a product
SonyStyle must display accurate inventory information.
# This is to make sure an invalidation is only trigged when a product becomes in stock or goes out of stock
# SPEC00090005226 = "Intel Core i7-2630QM quad-core processor (2.0GHz) with Turbo Boost up to 2.90GHz"
Scenario: US CTO component in stock changes quantity and remains in stock
Given we will receive a "GCTO" inventory feed
@fabrizioc1
fabrizioc1 / rvm_bash_prompt.sh
Last active September 29, 2015 14:07
RVM bash prompt
# Add this to your .bashrc after loading RVM
# Example:
# [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
# source $HOME/rvm_bash_prompt.sh
# Current RVM gemset for bash prompt
function __my_rvm_ruby_version {
local gemset=$(echo $GEM_HOME | awk -F'@' '{print $2}')
[ "$gemset" != "" ] && gemset="@$gemset"
local version=$(echo $MY_RUBY_HOME | awk -F'-' '{print $2}')
@fabrizioc1
fabrizioc1 / PolynomialTest.java
Created February 23, 2012 04:23
Polynomial Data Structure
/*
* Question: Create a data structure to represent polynomial addition and substraction
* Run the code online: http://ideone.com/s5qjU
*/
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;
public class PolynomialTest
{