This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a mixin module that adds support for tail-recursive | |
# style programming in Ruby without relying on any true tail | |
# recursion optimization in the Ruby virtual machine. | |
# | |
# Since tail recursion calls are actually deferred until after | |
# returning from the method that invoked #tail, recursions can | |
# be "nested" to an unlimited depth without overflowing the | |
# stack. | |
# | |
# The including module can define tail-recursive methods within |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'bundler' | |
require 'beaneater' | |
beanstalk = Beaneater::Pool.new(['localhost:11300']) | |
sleep_tube = beanstalk.tubes['sleeper-take-a-nap'] | |
sleep_tube.put '-' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fib = Fiber.new do |val| | |
puts ' -- 2 --' | |
puts 'fiber started & got: ' + val | |
puts 'now yield with "1st yield"' | |
val = Fiber.yield('fiber yield 1') | |
puts ' -- 4 --' | |
puts '1st yield got: ' + val | |
puts 'now yield with "2nd yield"' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./config/initializers/deferred_uniqueness_validations.rb | |
# | |
# This initializer adds support for deferred processing of | |
# ActiveRecord uniqueness validations. When a uniqueness | |
# validation is deferred using the :deferred => true option, it is | |
# not checked before save. Instead, if there is a uniqueness | |
# constraint defined for the column in the database, and if the | |
# save operation raises an ActiveRecord::RecordNotUnique | |
# exception, then the validation is performed so that the precise | |
# cause can be determined and the appropriate error information |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ArrayUtils | |
def self.flatten(array, result=[]) | |
array = Array(array) | |
array.each do |entry| | |
case entry | |
when Array | |
flatten entry, result | |
else | |
result << entry | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get ourselves a clean, top-level binding. | |
def main_binding | |
binding | |
end | |
module ModuleUtils | |
module ModuleMethods ; end | |
self.extend ModuleMethods | |
module ModuleMethods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
S=Struct.new(:a,:b) | |
ss = [S.new(1,2), S.new(3,4)] | |
p ss.map{|a,b| "#{a} #{b}" } | |
# => ["#<struct S a=1, b=2> ", "#<struct S a=3, b=4> "] | |
class S ; def to_ary ; to_a ; end ; end | |
p ss.map{|a,b| "#{a} #{b}" } | |
# => ["1 2", "3 4"] |
OlderNewer