Skip to content

Instantly share code, notes, and snippets.

@emirikol
emirikol / Cargo.toml
Last active May 2, 2024 08:46
coin game
[package]
name = "ant"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[[bin]]
#!/usr/bin/ruby
env = ARGV.shift
files = ARGV.map {|f| f.sub(/^\//, '') }.join(',')
Kernel.exec "cap #{env} deploy:upload FILES=#{files} deploy:restart"
@emirikol
emirikol / foo.rb
Created August 3, 2014 15:45
saving paperclip string directly
#option A)
stream = StringIO(string)
stream.content_type = 'application/pdf' #paperclip adds silly attr accessors to StringIO that we need to pass validations
#stream.original_filename = "anything.pdf" #if needed...
signed_pdf_document.original_pdf = stream
signed_pdf_document.save
#option B)
file = Tempfile.new( ["file_name", '.pdf'] )
def back_and_fore_color(color)
rgb = color.sub('#','').scan(/../).map {|c| c.to_i(16)}
fore_color = rgb.sum > 384 ? 'black' : 'white'
"background-color:#{color};color:#{fore_color};"
end
class Enum < Hash
def initialize(*members)
super()
@rev = {}
members.each_with_index {|m,i| self[i] = m }
end
def [](k)
super || @rev[k] # || raise ArgumentError, "#{k} is not a member of this enum"
end
def []=(k,v)
@emirikol
emirikol / gist:3076935
Created July 9, 2012 14:43
Association methods
#tl;dr - user :extend, embrace module explosion.
#delegates are a classic and simple tool here, they allow you to mass produce simple forwarding functions to be replaced as needed. this is exactly what LOD is all about.
# first of all, without knowing the code I'd reject the claim that knowing how to stop a subset of workers is the factory's concern rather than the Worker class (not instance). in which case - why pass a factory instance at all? the following will work.
class Factory
has_many :machines, :inverse_of=>:factory
delegate :stop, :start, :eat, :to=>machines, :prefix=>true
end