Tests build confidence. Write 'em. They'll save your ass, and they'll let you take a chainsaw to your code without being afraid of unintended consequences.
This file contains hidden or 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 String | |
def to_ascii_encode | |
old_kcode, $KCODE = $KCODE, 'ascii' | |
result = inspect[1..-2] | |
$KCODE = old_kcode | |
result | |
end | |
end |
This file contains hidden or 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 generic_select (item, default, object, method, options = {}) | |
obj = options[:object] || instance_variable_get("@#{object}") | |
value = options[:value] || (obj.nil? ? obj : obj.send(method)) | |
value = value.to_s | |
options[:size] = options[:size] || default unless default.blank? | |
text_field(object, method, options) + '<div class="' + item + '_autocomplete autocomplete"></div>' | |
end | |
def generic_tag_select (item, default, name, value = nil, options = {}) | |
value = value.to_s |
This file contains hidden or 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
# This is a script to install both ruby 1.8.7 and 1.9.1, with suffixed binaries and symlinks | |
mkdir -p /usr/local/src ; cd /usr/local/src | |
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p160.tar.bz2 | |
tar -xjvf ruby-1.8.7-p160.tar.bz2 | |
cd ruby-1.8.7-p160 | |
./configure --prefix=/usr --program-suffix=18 --enable-shared | |
make && sudo make all install | |
cd /usr/local/src |
This file contains hidden or 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 'wordlist' | |
# Get evil idea and swap in code words | |
print "Enter your new idea: " | |
idea = gets | |
Decoder::CodeWords.each do |real, code| | |
idea.gsub!( real, code ) | |
end | |
# Save the jibberish to a new file |
This file contains hidden or 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/ruby19 -wKu | |
# Here's the problem: Ruby is playing the part of a deaf grandma. If I | |
# don't ask her a question in upcase, she can't hear me. If I ask her any | |
# question in upcase, she'll respond in the same fashion unless I shout BYE | |
# to her. I need to shout BYE to her 3 times to escape her madness. | |
puts 'What is it dear?' | |
question = gets.chomp! | |
counter = 0 | |
p 'DEBUG: before everything' |
This file contains hidden or 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 summer_monthly_usage | |
@summer_monthly_usage ||= rates.summer_months_nbr.map{|month| monthly_usage[month - 1]}.inject(0){|sum, v| sum += v} | |
end |
This file contains hidden or 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('http') | |
.createServer(function(request, response){ | |
response.sendHeader(200, {'Content-Type' : 'text/plain'}); | |
response.sendBody("zoom"); | |
response.finish(); | |
}) | |
.listen(8000); |
This file contains hidden or 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
fib ↼ function | |
if({ @x = 0 | @x = 1 }, then: { @last(1) }, else: function) | |
@last( callee(@1 - 1, ?) + callee(@1 - 2, ?) ) | |
fib(5, { print(@) }) |
This file contains hidden or 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
foo bar | |
=> foo['bar'] | |
# assignment (no, this is impossible to handle in userspace without breaking/crippling lookup overloading) | |
... = ... | |
# methods (this is just syntactic sugar) | |
foo.bar |