Skip to content

Instantly share code, notes, and snippets.

View tomas-stefano's full-sized avatar

Tomas D'Stefano tomas-stefano

  • Ministry of Justice, Department for Education
  • London
View GitHub Profile
lib_dir = File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
@tomas-stefano
tomas-stefano / Rakefile
Created October 31, 2010 22:41
Rakefile from a C Library and Cgreen Tests
#
# Rakefile with a C Library with the following tree:
#├── library
#│   ├── something.c
#│   └── something.h
#├── specifications
#│   ├── all_specifications.c
#│   ├── all_specifications.o
#│   ├── something_spec.c
#│   └── something_spec.o
@tomas-stefano
tomas-stefano / dict.h
Created November 1, 2010 23:02
Dictionary from Redis
// dict.h
typedef struct dictEntry {
void *key;
void *val;
struct dictEntry *next;
} dictEntry;
typedef struct dictht {
dictEntry **table;
/* Hash Tables Implementation.
*
* This file implements in memory hash tables with insert/del/replace/find/
* get-random-element operations. Hash tables will auto resize if needed
* tables of power of two in size are used, collisions are handled by
* chaining. See the source code for more information... :)
*
*/
#include <stdlib.h>
>> env = RVM::Environment.new('1.9.2')
>> env.env_contents
=> "export PATH=\"/Users/tomas/.rvm/gems/ruby-1.9.2-p0/bin:/Users/tomas/.rvm/gems/ruby-1.9.2-p0@global/bin:/Users/tomas/.rvm/rubies/ruby-1.9.2-p0/bin:/Users/tomas/.rvm/bin:$PATH\"\nRUBY_VERSION='ruby-1.9.2-p0'\nexport RUBY_VERSION\nGEM_HOME='/Users/tomas/.rvm/gems/ruby-1.9.2-p0'\nexport GEM_HOME\nGEM_PATH='/Users/tomas/.rvm/gems/ruby-1.9.2-p0:/Users/tomas/.rvm/gems/ruby-1.9.2-p0@global'\nexport GEM_PATH\nBUNDLE_PATH='/Users/tomas/.rvm/gems/ruby-1.9.2-p0'\nexport BUNDLE_PATH\nMY_RUBY_HOME='/Users/tomas/.rvm/rubies/ruby-1.9.2-p0'\nexport MY_RUBY_HOME\nIRBRC='/Users/tomas/.rvm/rubies/ruby-1.9.2-p0/.irbrc'\nexport IRBRC\nrvm_ruby_string='ruby-1.9.2-p0'\nexport rvm_ruby_string\nunset rvm_gemset_name\nunset MAGLEV_HOME\n"
@tomas-stefano
tomas-stefano / set_timestamps.rb
Created November 11, 2010 13:31
Set Timestamps manually in Rails
###### One way
ActiveRecord::Base.record_timestamps = false
# set timestamps manually
ActiveRecord::Base.record_timestamps = true
###### Other way
@tomas-stefano
tomas-stefano / Linking Object files.rb
Created November 16, 2010 20:11
Extconf from relation library
require 'mkmf'
extension_name = 'relation_table'
dir_config(extension_name)
# Source files with header and *.c and *.o files
#
RELATION_HEADER_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..', 'source'))
RELATION_HEADER_FLAG = "-I #{RELATION_HEADER_PATH}"
@tomas-stefano
tomas-stefano / Results.rb
Created November 17, 2010 14:20
Arel benchmark
Simple Query
10.000 Queries 0.320000 0.000000 0.320000 ( 0.333302)
100.000 Queries 3.090000 0.010000 3.100000 ( 3.115193)
1.000.000 Queries 30.850000 0.080000 30.930000 ( 31.036174)
Add some little complexity =p
10.000 Queries 0.550000 0.000000 0.550000 ( 0.551271)
100.000 Queries 5.550000 0.020000 5.570000 ( 5.643197)
1.000.000 Queries 55.670000 0.180000 55.850000 ( 56.547011)
@tomas-stefano
tomas-stefano / Some file Encoding.rb
Created November 18, 2010 13:32
Puts encoding information about the file
open('some_file', "r:UTF-8") do |file|
puts file.external_encoding.name
p file.internal_encoding
file.each do |line|
p [line.encoding.name, line]
end
end
10.000 Queries 0.030000 0.000000 0.030000 ( 0.031400)
100.000 Queries 0.300000 0.010000 0.310000 ( 0.307062)
1.000.000 Queries 3.070000 0.060000 3.130000 ( 3.150164)
A Little more complexity =p
10.000 Queries 0.090000 0.000000 0.090000 ( 0.086945)
100.000 Queries 0.840000 0.020000 0.860000 ( 0.857581)
1.000.000 Queries 8.360000 0.200000 8.560000 ( 8.568261)