Skip to content

Instantly share code, notes, and snippets.

@ilake
ilake / trick.rb
Last active December 15, 2015 21:38
# DATA, __END__, pos
# http://shifteleven.com/articles/2009/02/09/useless-ruby-tricks-data-and-__end__
# http://stackoverflow.com/questions/1333720/ruby-scope-of-data-after-end
# You could lambda in when
case n
when lambda(&:even?)
puts "This number is even."
...
@ilake
ilake / force app encoding to UTF-8.rb
Created April 1, 2013 06:55
force app encoding to UTF-8
# config.ru
if RUBY_VERSION =~ /1.9/
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
end
# Understanding Ruby Blocks, Procs and Lambdas
# http://stackoverflow.com/questions/6372596/ruby-block-procs-and-instance-eval
# http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
# Metaprogramming Ruby: class_eval and instance_eval
# http://www.jimmycuadra.com/posts/metaprogramming-ruby-class-eval-and-instance-eval
my_proc = proc do
puts "yeaaaaah"
end
label_mapping = case state
when "abc", "def"
"label-success"
when "hij", "klm", "opq"
"label-important"
...
...
end
@ilake
ilake / gist:4982584
Created February 19, 2013 02:25
cronjob
PATH=/usr/local/bin:/usr/bin:/bin:$PATH
20 23 * * * cd /var/www/foo;RAILS_ENV=production bundle exec rake task:jobs auto_update=true >> /var/www/foo/log/foo_info_$(date +\%m\%d\%Y).log
PATH=/usr/local/bin:/usr/bin:/bin:$PATH
0 0 * * * cd /var/www/foo;RAILS_ENV=production bundle exec rake task:jobs auto_update=true from_date=$(date -dyesterday +\%m/\%d/\%Y) >> /var/www/foo/log/foo_info_$(date -dyesterday +\%m\%d\%Y).log

Contract Killer 3

Revised date: 07/11/2012

Between us [company name] and you [customer name]

Summary:

We’ll always do our best to fulfil your needs and meet your expectations, but it’s important to have things written down so that we both know what’s what, who should do what and when, and what will happen if something goes wrong. In this contract you won’t find any complicated legal terms or long passages of unreadable text. We’ve no desire to trick you into signing something that you might later regret. What we do want is what’s best for both parties, now and in the future.

@ilake
ilake / gist:4592019
Created January 22, 2013 04:23
sql UPDATE, SET
execute <<-SQL
UPDATE contacts
SET country =
CASE
WHEN country = 'USA' THEN 'United States'
WHEN country = 'Russia' THEN 'Russian Federation'
WHEN country = 'Virgin Islands' THEN 'Virgin Islands, U.S.'
END
WHERE country in ('USA', 'Russia', 'Virgin Islands')
SQL
@ilake
ilake / gist:4576375
Last active December 11, 2015 08:58
Hosted services for Ruby webapp
@ilake
ilake / gist:4527383
Last active December 11, 2015 01:59
def self.query_benchmark
keys = Visit.column_names
total = Visit.count
Benchmark.bm(7) do |x|
# 346.010000 1.380000 347.390000 (357.346086)
# 361.920000 1.650000 363.570000 (417.746442)
x.report("ActiveRecord") do
Visit.find_in_batches(:batch_size => 5000) do |visits|
visits.map{|v| keys.map{|k| v.send(k.to_sym)}}
end
# railties/lib/rails/engine.rb
# Define the Rack API for this engine.
def call(env)
app.call(env.merge!(env_config))
end
# We could find app from here
# Returns the underlying rack application for this engine.
def app
@app ||= begin