Skip to content

Instantly share code, notes, and snippets.

View unnu's full-sized avatar

Norman Timmler unnu

  • Hamburg, Germany
View GitHub Profile
@unnu
unnu / lru_cache.rb
Created July 20, 2009 08:14
Ruby LRU cache with tests
class LRUCache
def initialize(size = 10)
@size = size
@store = {}
@lru = []
end
def set(key, value = nil)
value = yield if block_given?
@unnu
unnu / app_config.rb
Created July 20, 2009 08:16
Rails AppConfig inplementation for application configuration
class AppConfig
instance_methods.each { |m| undef_method m unless m =~ /^__|is_a\?|empty?/ }
def initialize
@hash = Hash.new do |hash, key|
hash[key] = AppConfig.new unless @finalized
end
end
def method_missing(message, value = nil)
IDEA 1
(1..100).each {|i| puts [i,('foo' if i%3==0),('bar' if i%5==0)].join}
IDEA 2
(1..100).each {|i| puts "#{i}#{'foo' if i%3==0}#{'bar' if i%5==0}"}
(1..100).each{|i|puts"#{i}#{'foo'if i%3==0}#{'bar'if i%5==0}"} #shortened
@unnu
unnu / next_is_blocks_return.rb
Created May 30, 2010 15:31
next can be used tor return from a block with return value in Ruby
def foo
2.times do |i|
puts yield
puts "We will go here #{i}"
end
end
foo do
next "Next is the block's return"
"Never returns this"
@unnu
unnu / gist:437422
Created June 14, 2010 08:10
Dotted progress bar for shell scripts
require 'stringio'
class Progress
def initialize(show)
@device = StringIO.new unless show
end
def device
@device ||= $stdout
@unnu
unnu / Textmate File- and Folderpattern
Created September 8, 2010 07:18
Textmate Folder and File Patterns
File Pattern:
!\.(log)|\.(sp?)|(/\.(?!htaccess)[^/]*|\.(tmproj|o|pyc)|/Icon\r|/svn-commit(\.[2-9])?\.tmp)$
Folder Pattern:
!.*/(\.[^/]*|CVS|_darcs|_MTN|\{arch\}|blib|db/data.*|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$
@unnu
unnu / gist:1084701
Created July 15, 2011 13:37
Growl Logger for Rails Debug
if Rails.env.development?
GROWL = Growl.new("127.0.0.1", "ruby-growl", ["ruby-growl Notification"])
GROWL.class_eval do
def log(object)
GROWL.notify "ruby-growl Notification", "AskWhat Debug", object.inspect
Rails.logger.debug { object.inspect }
end
end
end
@unnu
unnu / gist:1303319
Created October 21, 2011 07:49
JRuby ThreadPoolExecutor with FutureTask showcase
require 'java'
# setup executor
java_import 'java.util.concurrent.ThreadPoolExecutor'
java_import 'java.util.concurrent.TimeUnit'
java_import 'java.util.concurrent.LinkedBlockingQueue'
java_import 'java.util.concurrent.FutureTask'
java_import 'java.util.concurrent.Callable'
core_pool_size = 5
@unnu
unnu / gist:1379254
Created November 19, 2011 19:30
Ringbuffer Implementation with Eventmachine
require 'rubygems'
require 'eventmachine'
require 'fiber'
class TcpReceiver < EM::Connection
attr_accessor :blocker
def initialize(receiver)
@receiver = receiver
super
class A
def self.hello
p 'hello'
end
def class
(class << self; self; end)
end
end