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 / 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"
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 / 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)
@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?