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
/* | |
A Goroutine safe pattern using channels that abstracts away the channels | |
This is a concept of creating a goroutine safe object that uses channels under the covers to communicate | |
with the internal map[string]string structure. I know that typically this kind of solution may done | |
with mutexes but the excercise was in using channels on purpose although they are heavier. | |
Note a couple of points: | |
- When using channels, you can still build a public-facing api that nicely abstracts them away, therefore |
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
package main | |
import ( | |
"io/ioutil" | |
"os" | |
"path/filepath" | |
"regexp" | |
"strings" | |
) |
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
# Poor Man's Fiber (API compatible Thread based Fiber implementation for Ruby 1.8) | |
# (c) 2008 Aman Gupta (tmm1) | |
unless defined? Fiber | |
require 'thread' | |
class FiberError < StandardError; end | |
class Fiber | |
def initialize |
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
package main | |
import "fmt" | |
type state struct { | |
board [7][6]int; | |
heights [7]int; | |
} | |
type move struct { |
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
def run_solr | |
defs = { | |
'jetty.home' => @path, | |
'solr.data.dir' => @path/'solr'/'data' | |
}.map { |k, v| "-D#{k}=#{v}" }.join(' ') | |
cmd = "java -jar #{@path}/start.jar #{@path/'etc'/'jetty.xml'} -classpath #{@path/'lib'} #{defs}" | |
# IO.popen is more portable than fork/exec (i.e. to JRuby) | |
pid = IO.popen(cmd, 'r').pid |
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 actually available as a gem: gem install rack-rewrite | |
# Full source code including tests is on github: http://github.com/jtrupiano/rack-rewrite | |
module Rack | |
# A rack middleware for defining and applying rewrite rules. In many cases you | |
# can get away with rack-rewrite instead of writing Apache mod_rewrite rules. | |
class Rewrite | |
def initialize(app, &rule_block) | |
@app = app | |
@rule_set = RuleSet.new |
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 Rack | |
class ChromeFrame | |
def initialize(app, options={}) | |
@app = app | |
end | |
def call(env) | |
status, headers, response = @app.call(env) | |
if env['HTTP_USER_AGENT'] =~ /MSIE/ && response.content_type == 'text/html' |
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
#http://www.infoq.com/cn/news/2009/10/21-ruby-web-middlewares | |
module Rack | |
class NoIE | |
def initialize(app, options = {}) | |
@app = app | |
@options = options | |
@options[:redirect] ||= 'http://www.microsoft.com/windows/internet-explorer/default.aspx' | |
@options[:minimum] ||= 7.0 | |
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
rd, wr = IO.pipe | |
if fork | |
wr.close | |
puts "Parent got: <#{rd.read}>" | |
rd.close | |
Process.wait | |
else | |
rd.close | |
puts "Sending message to parent" |