Skip to content

Instantly share code, notes, and snippets.

Sam Stokes samstokes

Block or report user

Report or block samstokes

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View synchronous_shim_example.rb
require 'em-http'
require 'hpricot'
class EM::HttpRequest
synchronous_shim! :get
# => "Sam Stokes"
samstokes / module_inclusion_overriding.rb
Created Jan 21, 2011
Specs for when module inclusion does and doesn't cause method overriding
View module_inclusion_overriding.rb
module Existing
def twinge
def prunge
# module included in another module
samstokes / synchronous_deferrable.rb
Created Dec 30, 2010
Example of using Deferrable to provide an asynchronous API to a synchronous method.
View synchronous_deferrable.rb
def do_funky_stuff
d =
# do some long-running thing synchronously
return d
do_funky_stuff.callback { sleep 5; puts 'Done!' } # blocks for 5 seconds
# prints "Done!"
samstokes / map_chain.rb
Created Nov 9, 2010
Map a series of methods successively, plus extra goodies
View map_chain.rb
require 'blankslate'
# Instead of writing
# > (1..3).map(&:even?).map(&:to_s).map(&:upcase).map(&:reverse)
# => ["ESLAF", "EURT", "ESLAF"]
# write the more concise
# > (1..3).map_chain.even?.to_s.upcase.reverse.result
samstokes / ip2int.rb
Created Nov 9, 2010
Converts an IP address from 'dotted string' representation to a 32-bit integer.
View ip2int.rb
def ip2int(dotted)
zip([24, 16, 8, 0]).
inject(0) do |s, (segment, shift)|
s + (segment << shift)
samstokes / chunked.rb
Created Nov 8, 2010
Splits an enumerable into chunks. One-liner for use in Heroku console.
View chunked.rb
module Enumerable; def chunked(n); i=0; inject([]) {|chunks, item| if i % n == 0; chunks << [item]; else; chunks.last << item; end; i += 1; chunks }; end; end
samstokes / gemver.rb
Created Sep 22, 2010
Returns the currently activated version of the named gem.
View gemver.rb
def gemver(gem); Gem.loaded_specs[gem.to_s].version; end
samstokes / equi.rb
Created Jan 8, 2010
Solution to Codility demo problem, finding equilibrium index of an array
View equi.rb
def sums_before(src)
dest = [0]
src.each_with_index do |item, index|
dest[index + 1] = dest[index] + item
def equi ( arr )
sums_left = sums_before(arr)
You can’t perform that action at this time.