Skip to content

Instantly share code, notes, and snippets.

View maxim's full-sized avatar
🛠️

Max Chernyak maxim

🛠️
View GitHub Profile
#!/usr/bin/env ruby
# Usage:
# Example command ./coverflowify.rb source.png 30 20 50 true output.png
#
# The values are:
# * shrink percent - specifies by how much percent to shrink the background images
# * skew percent - specifies how much 3d rotation (inward) to give background images
# * visible percent - specifies how much of background images stick out on left and right
# * add_shadow - if true, adds default shadow, also accepts full shadow spec, like "70x4+5+5"
@maxim
maxim / foo.rb
Last active December 11, 2015 22:28
Wat is this? (Ruby 1.9.3p327)
require 'minitest/autorun'
class FooTest < MiniTest::Unit::TestCase
def test_foo
assert_equal('0x0', '0x1')
end
end
# Running tests:
@maxim
maxim / gist:4211278
Created December 5, 2012 01:43
Why tempfiles disappear randomly
>> GC.disable
=> false
>>
>> def foo
>> file = Tempfile.new('test')
>> file.path
>> end
=> nil
>>
>> path = foo
a = { foo: 'foo', bar: 'bar' }
b = { foo: 'fooo' }
# Reconcile duplicate keys
a.merge(b) {|k, old_v, new_v| [old_v, new_v].min}
# => { foo: 'foo', bar: 'bar' }
# Ever wanted Hash#map to return a hash? This is a workaround
a.merge(a) {|k, v| v.upcase}
# => { foo: "FOO", bar: "BAR" }
@maxim
maxim / gist:4014181
Created November 4, 2012 22:51
Ruby's merge calls to_hash
>> obj = Object.new
=> #<Object:0x007ff6bd6b07e0>
>> def obj.to_hash; { bar: 'bar' } end
=> nil
>> { foo: 'foo' }.merge(obj)
=> {:foo=>"foo", :bar=>"bar"}
@maxim
maxim / gary.rb
Created October 4, 2012 04:51 — forked from radar/gary.rb
["a b.c d.e"[/\.(\S+)$/, 1].split('.'), $`].reverse
@maxim
maxim / gist:3824613
Created October 3, 2012 02:36
EU West S3 with paperclip
# Something like this in initializer
Paperclip.interpolates(:s3_eu_url) { |attachment, style|
"#{attachment.s3_protocol}://s3-eu-west-1.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
}
# And then this in attachment options
url: ':s3_eu_url',
storage: :s3,
@maxim
maxim / query_trace.rb
Created August 8, 2012 05:58
with_query_trace method for logging SQL call traces in specific parts of code, without restarting server
if Rails.env.development?
module Kernel
def with_query_trace
Rails.backtrace_cleaner.add_silencer do |line|
line.include?('query_trace.rb')
end
callback = lambda do |*args|
bt = Rails.backtrace_cleaner.clean(caller[1..-1])
@maxim
maxim / tm2iterm.rb
Created June 10, 2012 04:04
Convert TextMate themes into iTerm 2 color schemes.
#!/usr/bin/env ruby
#
# This script is an astonishing feat of top notch
# rockstar craftsmanship. It totally uses artificial
# intelligence to extract colors out of tmTheme and
# build an itermcolors scheme file for iTerm2.
#
# I know this sounds crazy, but it actually knows
# approximately what colors should be used in the
# ANSI list, and tries to find nearest colors from
@maxim
maxim / spec_helper.rb
Created June 9, 2012 18:14
Making after_commit play nice with use_transactional_fixtures.
# This is an example of how to use database_cleaner gem and
# RSpec tags to make `after_commit` hook play nice with
# `use_transactional_fixtures`.
# Simply mark the specs that use after_commit with
# `:uses_after_commit` tag.
# ...
require 'database_cleaner'