Skip to content

Instantly share code, notes, and snippets.

Steve Jorgensen stevecj

Block or report user

Report or block stevecj

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@stevecj
stevecj / flatten-it.rb
Last active Mar 30, 2016
Coding exercise: Flatten an array in Ruby w/o using `Array#flatten`
View flatten-it.rb
module ArrayUtils
def self.flatten(array, result=[])
array = Array(array)
array.each do |entry|
case entry
when Array
flatten entry, result
else
result << entry
end
@stevecj
stevecj / gist:9ace6a70370f6d1a1511
Last active Apr 8, 2018
Ruby will destructure objects that are not arrays, but respond to #to_ary
View gist:9ace6a70370f6d1a1511
S=Struct.new(:a,:b)
ss = [S.new(1,2), S.new(3,4)]
p ss.map{|a,b| "#{a} #{b}" }
# => ["#<struct S a=1, b=2> ", "#<struct S a=3, b=4> "]
class S ; def to_ary ; to_a ; end ; end
p ss.map{|a,b| "#{a} #{b}" }
# => ["1 2", "3 4"]
@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 / 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 / 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 / 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 / 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 / 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 / deferred_uniqueness_validations.rb
Created Mar 7, 2013
Allow any ActiveRecord uniqueness validation(s) to be deferred, and only check after a failed save due to uniqueness violation error from the database.
View deferred_uniqueness_validations.rb
# ./config/initializers/deferred_uniqueness_validations.rb
#
# This initializer adds support for deferred processing of
# ActiveRecord uniqueness validations. When a uniqueness
# validation is deferred using the :deferred => true option, it is
# not checked before save. Instead, if there is a uniqueness
# constraint defined for the column in the database, and if the
# save operation raises an ActiveRecord::RecordNotUnique
# exception, then the validation is performed so that the precise
# cause can be determined and the appropriate error information
@stevecj
stevecj / fiber_demo.rb
Created Feb 4, 2013
A demonstration of transfer of control to/from a Fiber in Ruby 1.9.x.
View fiber_demo.rb
fib = Fiber.new do |val|
puts ' -- 2 --'
puts 'fiber started & got: ' + val
puts 'now yield with "1st yield"'
val = Fiber.yield('fiber yield 1')
puts ' -- 4 --'
puts '1st yield got: ' + val
puts 'now yield with "2nd yield"'
You can’t perform that action at this time.