Skip to content

Instantly share code, notes, and snippets.

View floere's full-sized avatar
😁
Open Sourcing

Florian R. Hanke floere

😁
Open Sourcing
View GitHub Profile
@floere
floere / ends.rb
Created March 27, 2012 19:59
The END block in Ruby.
END { p "This is the end, my only friend, the end." }
p "Feeling very sleepy..."
sleep 1
@floere
floere / sinatra_regexp_suggestion.rb
Created March 22, 2012 01:16
A regexp to match named groups in a URL
# I've looked at the case at hand which is:
# foo.bar -> foo: foo.bar
# foo.bar -> foo: foo, format: bar
# This roughly translates to:
# For each part (foo, format), match as many possible subexpressions consisting of multiple word characters or one non-word characters
# (we might say \. explicitly, in this specific case). Do this lazily, except for the last part, since that one needs to gobble up the rest.
# And: If we have two or more parts, join them by a lazy match of a dot (\.?), which is not included in the named group.
@floere
floere / master_child.rb
Created January 8, 2012 09:20
An example of Child/Parent communication.
require 'yajl'
class MasterChild
def initialize
@child, @parent = IO.pipe
start_master_process_thread
end
# This runs a thread that listens to child processes.
@floere
floere / gist:1503691
Created December 20, 2011 22:54
ActiveRecord Integration
class Book < ActiveRecord::Base; end
Book.establish_connection YAML.load(File.open('db.yml'))
@floere
floere / io.rb
Created November 10, 2011 12:47
string_io = StringIO.new(...)
class << string_io
def overridden_method
# ...
end
end
@floere
floere / a_picky_static_site_search.rb
Created November 1, 2011 22:17
An example of how to use Picky to search your html data.
# Runs with Picky 3.3.1.
#
# Note: You need some example html files in
# the same directory.
#
# Index data and start:
# 1. rake index
# 2. ruby app.rb
#
# Then, search on it using:
require '/Users/admin/temp/picky/server/lib/picky'
t0 = Time.now
class Tweet
attr_reader :id, :user, :text
def initialize id, user, text
@id, @user, @text = id, user, text
end
@floere
floere / dumping_indexes.rb
Created October 30, 2011 04:38
How to dump the indexes after e.g. realtime indexing.
# Available from Picky 3.2.1.
#
require 'picky'
index = Picky::Index.new :dump_test do
category :name
end
Thing = Struct.new :id, :name
index.replace Thing.new(1, 'Picky')
@floere
floere / picky_in_a_script.rb
Created October 26, 2011 06:29
Running Picky e.g. in a single script.
# Possible since Picky 3.2.0.
#
require 'picky'
include Picky
Thing = Struct.new :id, :name
index = Index.new :test do
category :name, similarity: Similarity::DoubleMetaphone.new(3)
@floere
floere / decoration_using_extend.rb
Created September 7, 2011 12:08
Decoration using extend
module Decoration
def something
p :in_decoration_calling_super
super
p :after_super
end
end