Skip to content

Instantly share code, notes, and snippets.

@aprescott
aprescott / proc_equality_1.8.7.rb
Created April 1, 2011 23:09
Proc#== behaviour.
#
# 1.8.7 docs: http://ruby-doc.org/core-1.8.7/classes/Proc.html#M000463
#
# Return true if prc is the same object as other_proc, or if they are both procs with the same body.
#
RUBY_DESCRIPTION #=> ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-linux]
proc { 1 } == proc { 1 } # => false
proc { 1 + 1 } == proc { 1 + 1 } # => false
@aprescott
aprescott / local_variable_references.rb
Created April 5, 2011 12:49
Finding local variable references within the current scope
>> local_variables
=> ["_"]
>> a = ""
=> ""
>> local_variables
=> ["_", "a"]
>> b = binding; local_variables.select { |var| b.eval(var).object_id == a.object_id }
=> ["a"]
>> x = a
=> ""
@aprescott
aprescott / pdf_bombs.html
Created April 14, 2011 22:08
Uses HTML5 data-* attributes and old-school CSS content to get around that problem of PDF links causing browser hangs because you didn't check the URL.
<!-- See it in action: http://jsfiddle.net/eVAg4/1/ -->
<!doctype html>
<head>
<meta charset="UTF-8">
<title>PDF link file sizes in CSS</title>
<style type="text/css">
a[href$=".pdf"]::after { content: " (PDF)"; }
a[href$=".pdf"][data-size]::after { content: " (PDF, " attr(data-size) ")"; }
</style>
@aprescott
aprescott / gist:960903
Created May 7, 2011 22:19
Find the lowest multiple of some number greater than or equal to some other number.
def foo(n, m)
return n if n % m == 0
n + m - (n % m)
end
foo(25, 7) #=> 28
foo(28, 7) #=> 28
foo(29, 7) #=> 35
# No branching.
@aprescott
aprescott / simple_fiber_concurrency.rb
Created May 13, 2011 18:06
Simple example of concurrency in Ruby with Fiber.
require "fiber"
f1 = Fiber.new { |f2| f2.resume Fiber.current; while true; puts "A"; f2.transfer; end }
f2 = Fiber.new { |f1| f1.transfer; while true; puts "B"; f1.transfer; end }
f1.resume f2
@aprescott
aprescott / fullwidther.rb
Created May 29, 2011 01:07
Convert strings to their fullwidth versions
# it turns out that "A".ord - "A".ord is constant when you replace the two characters
# with, say "B" and "B", so ("B".ord + DIFFERENCE).to_s(16) == "ff22" and "\uff22"
# is "B". (Thanks, Unicode consortium.)
#
# an exception is U+0020 space, which has IDEOGRAPHIC SPACE U+3000, but the two don't
# differ by DIFFERENCE == 65248 == 0xfee0, so you have to replace 20+65248 == 0xff00
# with 0x3000.
DIFFERENCE = 65248
@aprescott
aprescott / random_stream.rb
Created August 28, 2011 17:07
Simple Enumerable extension to randomly stream elements.
module Enumerable
def random_stream(&block)
enumerator = Enumerator.new do |y|
loop do
y << sample
end
end
block ? enumerator.each(&block) : enumerator
end
@aprescott
aprescott / linked_list_reversal.rb
Created September 28, 2011 19:58
Linked list reversal
class Node
attr_accessor :value, :next_element
def initialize(value, next_element = nil)
@value = value
@next_element = next_element
end
def to_s
return value.to_s if @next_element.nil?
@aprescott
aprescott / object_monitor.rb
Created December 1, 2011 09:24
Monitor an object with delegation.
# BasicObject from
# simonecarletti.com/blog/2010/05/understanding-ruby-and-rails-proxy-patter-delegation-and-basicobject/
class BasicObject
instance_methods.each do |m|
undef_method(m) if m.to_s !~ /^__|^nil\?$|^send$|^object_id$/
end
end
class ObjectMonitor < BasicObject
attr_reader :object
@aprescott
aprescott / liquid_ordinals.liquid
Last active December 13, 2015 20:08
Ordinals in straight Liquid templates.
{% assign d = time | date: "%d" | plus:0 %}
{% assign d_mod = d | modulo:10 %}
{% assign ordinals = "th,st,nd,rd,th,th,th,th,th,th" | split:"," %}
{% if d > 10 and d < 14 %}
{{ d }}th
{% else %}
{{ ordinals[d_mod] }}
{% endif %}