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
# encoding: utf-8 | |
3 * 2 * 2 * 2 * 2 #=> 48 | |
3 * (2 ** 4) #=> 48 | |
3.send_repeatedly(4, :*, 2) #=> 48 | |
3.apply_repeatedly(4) { |obj| obj * 2 } #=> 48 | |
BasicObject.singleton_class.superclass # => Class | |
BasicObject.singleton_class.superclass.superclass.superclass.superclass #=> BasicObject | |
BasicObject.singleton_class.send_repeatedly(4, :superclass) #=> BasicObject |
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
# encoding: utf-8 | |
class HashWithSymbolAccessOfStrings < Hash | |
def [](key) | |
begin | |
self.fetch key | |
rescue KeyError | |
string_key = key.to_s | |
if string_key != key | |
self[string_version] |
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
# encoding: utf-8 | |
# examples: | |
nested_hashes = {:a=>{:b=>{:c=>{:more=>"answer"}}}, :num=>42} | |
path = [:a, :b, :c, :more] | |
nested_hashes.fetch_recursively(path) #=> "answer" | |
nested_hashes.fetch_recursively([:num]) #=> 42 | |
# wrong: | |
nested_hashes.fetch_recursively(:a) # raises NoMethodError; should pass an Array | |
nested_hashes.fetch_recursively([:num, :something]) # raises NoMethodError; can’t fetch from 42 |
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
# encoding: utf-8 | |
require 'rubygems' unless defined? Gem | |
# enables variable-name tab-completion | |
# I don’t care about the other features in here; I think irbtools already | |
# provides them, but there’s little harm in leaving them in | |
# from http://www.genlinux.org/2008/12/autocomplete-in-irb.html | |
IRB.conf[:AUTO_INDENT] = true | |
IRB.conf[:USE_READLINE] = true |
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
# encoding: utf-8 | |
class Hash | |
def with_new_default(*hash_new_args, &hash_new_block) | |
empty_defaulting_hash = Hash.new(*hash_new_args, &hash_new_block) | |
# using #merge; #replace would also replace the default value | |
return empty_defaulting_hash.merge(self) | |
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
#!/usr/bin/env bundle exec ruby | |
puts "hello" |
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
sudo apt-get install -y openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion g++ openjdk-6-jre-headless |
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 './main-tester' | |
puts "done in caller" |
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
#encoding: utf-8 | |
# http://news.ycombinator.com/item?id=4086325 | |
# … try to generate a table of how likely it was to get various dice rolls when you rolled 4 6-sided dice and took the top 3. | |
require 'pp' | |
def dice_roll(sides) | |
rand(sides) + 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
import scala.collection.mutable.ArrayBuffer | |
object Runner { | |
def main(args: Array[String]) { | |
// printFizzBuzz(20) | |
val max = 20 | |
val factorsAndWords = Map(3 -> "Fizz", 5 -> "Buzz") | |
printGenericFizzBuzz(max, factorsAndWords) | |
} |