Skip to content

Instantly share code, notes, and snippets.

@brianstorti
brianstorti / basic_mixin.rb
Created April 27, 2011 03:08
Basic mixin example
module Debug
def who_am_i?
self.class.name
end
end
class A
include Debug
end
@brianstorti
brianstorti / mixin_encapsulate.rb
Created April 27, 2011 03:33
Mixin to encapsulate functionality
module Summable
def sum
inject{ |i,j| i + j }
end
end
class Array
include Summable
end
@brianstorti
brianstorti / ubuntu_fix.txt
Created May 2, 2011 14:49
Ubuntu 11.04 ç
For those having trouble with 'ç' on Ubuntu 11.04:
Open gtk.immodules
$ sudo vim /usr/lib/gtk-2.0/2.10.0/gtk.immodules
Edit the following line:
"cedilla" "Cedilla" "gtk20" "/usr/share/locale" "az:ca:co:fr:gv:oc:pt:sq:tr:wa"
@brianstorti
brianstorti / selection_sort.rb
Created May 3, 2011 13:19
Selection Sort in ruby
# Selection sort (very slow on large lists)
a = [9,8,6,1,2,5,4,3,9,50,12,11]
n = a.size - 1
n.times do |i|
index_min = i
(i + 1).upto(n) do |j|
index_min = j if a[j] < a[index_min]
@brianstorti
brianstorti / insertion_sort.rb
Created May 3, 2011 14:41
Insertion sort in ruby
# Insertion sort (inefficient on large lists)
require 'test/unit'
def sort(a)
n = a.size - 1
1.upto(n) do |i|
new_value = a[i]
j = i
while j > 0 && a[j - 1] > new_value do
@brianstorti
brianstorti / bubble_sort.rb
Created May 3, 2011 18:12
Bubble sort in ruby
# Bubble sort (inefficient for sorting large data volumes)
require 'test/unit'
def sort(a)
swapped = true
n = a.size
j = 0
while swapped do
swapped = false
cd ~/Dropbox
mkdir -p repos/myrepo.git
cd !$
git --bare init
cd ~/Projects/myrepo
git remote add dropbox file://$HOME/Dropbox/repos/myrepo.git
git push dropbox master
@brianstorti
brianstorti / select_reject_collect.rb
Created May 29, 2011 17:19
Ruby - select x reject x collect
a = [1,2,3,4]
a.select{|n| n > 2} # 3,4
a.reject{|n| n > 2} # 1,2
a.collect{|n| n * 2} # 2,4,6,8
@brianstorti
brianstorti / dbi.rb
Created May 30, 2011 02:07
DBI example
require 'dbi'
begin
dbh = DBI.connect("DBI:Mysql:test:localhost", "root", "")
row = dbh.select_one("SELECT VERSION()")
puts "Server version: #{row[0]}"
rescue DBI::DatabaseError => e
puts "An error occurred"
ensure
dbh.disconnect if dbh
@brianstorti
brianstorti / gist:1006440
Created June 3, 2011 14:46
Install curb Windows
#DevKit
Download DevKit at http://rubyinstaller.org/downloads
ruby dk.rb init
ruby dk.rb review
ruby dk.rb install
#curl
Download curl at http://www.gknw.net/mirror/curl/win32/ (devel)