Skip to content

Instantly share code, notes, and snippets.

@andruby
andruby / risk.rb
Created May 11, 2012 01:46
Risk Simulation: Attacker or Defender advantage?
def game(attackers = 3, defenders = 2) # returns [attacker, defender] loses
attack = (1..attackers).map { rand(6) }.sort.reverse[0..1].reverse
defense = (1..defenders).map { rand(6) }.sort
max_wins = [attackers, defenders].min
attack_wins = (1..max_wins).map { attack.pop > defense.pop }.select{ |x| x}.count
[max_wins - attack_wins, attack_wins]
end
def full_game(armies = 30)
attack = defense = armies
while [attack-1, defense].min > 0
@andruby
andruby / 1-encodings.rb
Created April 18, 2012 16:31
Ruby Encodings for ruby 1.9.3p125
# ruby -rpp -e 'pp Encoding.list.map(&:name)'
["ASCII-8BIT",
"UTF-8",
"US-ASCII",
"Big5",
"Big5-HKSCS",
"Big5-UAO",
"CP949",
"Emacs-Mule",
"EUC-JP",
@andruby
andruby / ubuntu-11.10-gems.erb
Created March 27, 2012 20:51 — forked from kashif/ubuntu-11.10-gems.erb
Chef bootstrap With rvm and ruby 1.9.3 on ubuntu 11.10
bash -c '
<% if knife_config[:bootstrap_proxy] -%>
(
cat <<'EOP'
<%= "proxy = #{knife_config[:bootstrap_proxy]}" %>
EOP
) > ~/.curlrc
<% end -%>
if [ ! -f /usr/bin/chef-client ]; then
@andruby
andruby / backtrace
Created February 14, 2012 22:03
Nanoc 3 error
> nanoc deploy --target default
Captain! We’ve been hit!
=== MESSAGE:
LoadError: cannot load such file -- fog
=== COMPILATION STACK:
@andruby
andruby / inline-unpack.rb
Created December 30, 2011 17:50
ruby-inline c version of unpack for L,S and d.
inline(:C) do |builder|
builder.c "
VALUE c_unpack_l(char *bytes) {
uint32_t tmp;
memcpy(&tmp, bytes, sizeof(uint32_t));
return INT2NUM(tmp);
}"
builder.c "
VALUE c_unpack_s(char *bytes) {
uint16_t tmp;
@andruby
andruby / fancy_debug.rb
Created December 6, 2011 15:40
Fancy Debug
class ActiveSupport::BufferedLogger
# Print a logger.debug line with colored Name
# If the msg is not a string, it will inspect it
# Pass it a block to record the time_taken to yield it
# Symbols in the msg like :method will be interpolated with block_result.send(:method)
# eg: fancy_debug("Find Users", "Found :count users") { User.where() }
def fancy_debug(name, msg = '', &block)
if block_given?
start_time = Time.now
result = yield
@andruby
andruby / 1-vapor.rb
Created December 1, 2011 15:47
Vapor: Validate Part Of Resource
# VAlidate Part Of Resource
module Vapor
extend ActiveSupport::Concern
included do
class_attribute :target_name, :attribute_names
include ActiveModel::Validations
include ActiveModel::Conversion
include ActiveModel::Naming
end
@andruby
andruby / gist:1265160
Created October 5, 2011 17:57
Force disable net/http ssl certificate validation
# Dirty monkey patch to globally disable certificate validation
# YOU SHOULD NEVER USE THIS
# Except when testing/debugging with self signed certificates
require 'net/http'
module OpenSSL
module SSL
VERIFY_PEER = VERIFY_NONE
end
end
@andruby
andruby / comparison.rb
Created October 5, 2011 16:52
ruby comparison benchmark
require 'benchmark'
def is_it_true?
true
end
CONSTANT = 1
BenchTimes = 1_000_000
Benchmark.bm(20) do |bm|
@andruby
andruby / footnotes.rb
Created August 17, 2011 17:47
Rails-footnotes fixes for Rails 3.1 (put it in an initializer)
if defined?(Footnotes) && Rails.env.development?
Footnotes.run!
Footnotes::Filter.notes -= [:cookies, :session]
Footnotes::Notes::AssignsNote.ignored_assigns += [:@_view_renderer, :@_response_body, :@_routes, :@_view_context_class]
end