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 magic(): | |
x = 42 | |
ret = [] | |
for i in range(3): | |
ret.append(lambda: x) | |
x += 1 | |
return ret | |
fs = magic() | |
print fs[0]() |
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
<?php | |
// This class impersonates other objects and logs what they do. | |
class WithLogging { | |
// The parameter customMessage is a function that knows something about | |
// callee and can give extra interesting information about its state. | |
function __construct($callee, $customMessage = NULL) { | |
$this->callee = $callee; | |
$this->customMessage = $customMessage ?: function($obj) { return ''; }; | |
} |
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 word(n) | |
divisors = { | |
:vigintillion => 10**63, :novemdecillion => 10**60, | |
:octodecillion => 10**57, :septendecillion => 10**54, | |
:sexdecillion => 10**51, :quindecillion => 10**48, | |
:quattuordecillion => 10**45, :tredecillion => 10**42, | |
:duodecillion => 10**39, :undecillion => 10**36, | |
:decillion => 10**33, :nonillion => 10**30, | |
:octillion => 10**27, :septillion => 10**24, | |
:sextillion => 10**21, :quintillion => 10**18, |
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 pascal(row, pos) | |
throw :badArguments if pos > row or pos < 0 or row < 0 | |
def fact(n); 1.upto(n).inject(:*) || 1; end | |
fact(row) / (fact(pos) * fact(row - pos)) | |
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
def partitions(set_): | |
if not set_: | |
yield [] | |
return | |
for i in xrange(2**len(set_)/2): | |
parts = [set(), set()] | |
for item in set_: | |
parts[i&1].add(item) | |
i >>= 1 | |
for b in partitions(parts[1]): |
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'colorful' | |
url = "http://www.etymonline.com/index.php?search=#{ARGV[0]}" | |
doc = Nokogiri::HTML(open(url)) | |
doc.css('dl').search('dt').each do |node| | |
puts "#{node.text}".white |
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 cycles(perm): | |
remain = set(perm) | |
result = [] | |
while len(remain) > 0: | |
n = remain.pop() | |
cycle = [n] | |
while True: | |
n = perm[n] | |
if n not in remain: | |
break |
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'colorful' | |
url = "http://www.etymonline.com/index.php?search=#{ARGV[0]}" | |
doc = Nokogiri::HTML(open(url)) | |
doc.css('dl').search('dt').each do |node| | |
puts "#{node.text}".white |
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
$ rake spec | |
Run options: include {:focus=>true} | |
All examples were filtered out; ignoring {:focus=>true} | |
1) Excon hook when Excon's streaming API is used properly records and plays back the response | |
Failure/Error: recorded.should eq(played_back) | |
expected: "" | |
got: "FOO!" | |
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 'nokogiri' | |
require 'open-uri' | |
unless [1,2].include? ARGV.length | |
puts "Usage: arrest template-url [css-selector]" | |
exit 1 | |
end | |
def relax node, indent = 0 | |
tab = ' ' |
OlderNewer