This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> local_variables | |
=> ["_"] | |
>> a = "" | |
=> "" | |
>> local_variables | |
=> ["_", "a"] | |
>> b = binding; local_variables.select { |var| b.eval(var).object_id == a.object_id } | |
=> ["a"] | |
>> x = a | |
=> "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Enumerable | |
def random_stream(&block) | |
enumerator = Enumerator.new do |y| | |
loop do | |
y << sample | |
end | |
end | |
block ? enumerator.each(&block) : enumerator | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
OlderNewer