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 monkeypatch_content_type.rb
# Override Paperclip's content-type guessing support to add more MIME types.
# We actually have to monkey-patch File to do this, because Paperclip
# monkey-patches File to add #content_type (from module Paperclip::Upfile).
class File
def content_type_with_extras
type = (self.path.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
case type
when "pdf" then "application/pdf"
# add more content types here
else content_type_without_extras
samstokes / regex-groups-global.rb
Created Nov 18, 2009
How to extract groups from a regex match in Ruby without globals or temporary variables. Code snippets supporting
View regex-groups-global.rb
if "" =~ /@(.*)/
raise "bad email"
# => ""
samstokes / regex-snippet.rb
Created Nov 20, 2009
Code snippet from that taught me something new about Ruby syntax and regex usage.
View regex-snippet.rb
caller[0][/`([^']*)'/, 1]
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.