Skip to content

Instantly share code, notes, and snippets.

@stevecj
stevecj / trinary.rb
Last active Aug 29, 2015
Stoopid Ruby trick <g>
View trinary.rb
class Trinary
class << self
def ~ ; ~new ; end
def -@ ; -new ; end
def +@ ; +new ; end
end
def initialize(digits=[])
@digits = digits
end
@stevecj
stevecj / yaml_value_extraction_benchmarks.rb
Last active Aug 29, 2015
Benchmarking of ways of extracting single value from large YAML map (e.g. ActiveRecord serialized data)
View yaml_value_extraction_benchmarks.rb
require 'yaml'
require 'benchmark'
def make_yaml_string(num_entries=500)
entry_nums = (0...num_entries).to_a
entry_nums.shuffle!
result = ''
entry_nums.each do |n|
result << "something_#{n}: #{Random.rand}\n"
end
@stevecj
stevecj / require_limit_for_mysql_text.rb
Last active Aug 29, 2015
Require a :limit option for MySQL text columns created in Rails 3 app so don't unintentionally make 64K column that silently truncates.
View require_limit_for_mysql_text.rb
# == config/initializers/require_limit_for_mysql_text.rb ==
# Fail when an attempt is made to specify a MySQL text
# columnn in a migration without providing a :limit value.
# This helps to prevent accidentally defining a 64K column
# (default) when a 16MB or 4GB coumn is desired.
# Note that MySQL silently truncates the data when it is
# too long for the column, so problems are only apparent
# later when attempting to retrieve the data (if then).
@stevecj
stevecj / colortest
Created Jul 29, 2014
ANSI color test written in Ruby
View colortest
#!/usr/bin/env ruby
ColorNameSequence = %w[
Black
Red
Green
Yellow
Blue
Magenta
Cyan
@stevecj
stevecj / reversible_queue.rb
Last active Aug 29, 2015
ReversibleQueue: Works much like Ruby's native Queue, but provides an #undeq method, which Ruby's own Queue does not have
View reversible_queue.rb
# Works much like Ruby's native Queue, but provides an #undeq
# method, which Ruby's own Queue does not have.
class ReversibleQueue
def initialize
@queue_array = []
@threads_waiting = []
@mutex = Mutex.new
end
# Push an object onto the tail of the queue
@stevecj
stevecj / n-is-the-loneliest-number.txt
Last active Sep 10, 2015
(n) is the Loneliest Number
View n-is-the-loneliest-number.txt
One is the loneliest number that you'll ever do
Two can be as bad as one, well it's the loneliest number since the number one
Three is really not so bad, but it can still be lonely, and that's really sad
Four is really no big deal, so if you think it's lonely, then you'd best get real
Five is practically a crowd, and if you're tryin' to think, it can seem really loud.
Five is the loneliest number
Five is the loneliest number
Five is the loneliest number since the number four dum dum-dum dum-dum
@stevecj
stevecj / fieldset-alternatives.html
Created Jul 15, 2011
Spike of JS for forms with nested select-driven alternative fieldsets
View fieldset-alternatives.html
<!DOCTYPE html>
<html>
<head>
<style>
fieldset { display: block; }
.alternative { display:none; }
.alternative-selected { }
</style>
@stevecj
stevecj / enhanced_attributable.rb
Last active Sep 30, 2015
Ruby attr_accessor enhancement
View enhanced_attributable.rb
# Include this module in a class to add enhanced attr_accessor
# functionality. With this module included, ...
#
# * You may provide a block to an attr_accessor call that accepts
# a base attribute name and returns a default value for each
# instance attribute.
# * Any attribute name consisting of a base name followed by a
# "?" suffix represents a boolean attribute.
#
# If the including class has its own #initialize method, that
@stevecj
stevecj / ruby_module_nesting.rb
Created Mar 13, 2012
Ruby module nesting and scope of constant names
View ruby_module_nesting.rb
# == Module name assignment ==
# A Ruby module's name is determined when it first becomes
# assigned as the value of a top-level constant or of a
# constant in a module that has received a name.
# The module's name represents the hierarchy of containership,
# and is not dependent upon the execution path of the code
# that defined the module (as opposed to the module nesting
# for defined methods -- see below).
p [self, self.class, Module.nesting] # => [main, Object, []]
@stevecj
stevecj / defines_interface.rb
Created Oct 11, 2012
Interfaces for Ruby
View defines_interface.rb
# A module for inclusion in a class of objects that delegate to and
# provide restricted interface definitions for underlying "occurrence"
# objects.
#
# This is useful for purposes such as to enforce the same API for a
# unit under test as for mocks and stubs of the same unit used for
# testing other units.
#
# This is a proof of concept demonstration and is not well-tested,
# production-ready code.
You can’t perform that action at this time.