Skip to content

Instantly share code, notes, and snippets.

View roryokane's full-sized avatar

Rory O’Kane roryokane

View GitHub Profile
@roryokane
roryokane / examples.rb
Created August 1, 2012 18:32
Object#apply_repeatedly and #send_repeatedly for Ruby
# 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
@roryokane
roryokane / symbol_access_hash.rb
Created July 31, 2012 19:58
Hash with symbol access of Strings in Ruby – the start of an implementation
# 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]
@roryokane
roryokane / examples.rb
Created July 31, 2012 19:56
Hash#fetch_recursively for Ruby
# 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
@roryokane
roryokane / .irbrc
Created July 11, 2012 01:32
.irbrc – my Ruby IRB config
# 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
@roryokane
roryokane / hash_default_setting.rb
Created July 6, 2012 00:05
Ruby Hash monkey-patch methods relating to the hash’s default
# 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
@roryokane
roryokane / bundle-exec.rb
Created July 5, 2012 15:36
Ruby shebang line tests
#!/usr/bin/env bundle exec ruby
puts "hello"
@roryokane
roryokane / command.sh
Created July 2, 2012 20:18
Ubuntu command to install Ruby standard libraries
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
@roryokane
roryokane / main-tester-caller.rb
Created June 18, 2012 16:25
`self.inspect == 'main'` is no substitute for `__FILE__ == $0`
require './main-tester'
puts "done in caller"
@roryokane
roryokane / higher_dice_rolls.rb
Created June 9, 2012 02:24
likelihood of dice rolls when you roll four six-sided dice, take the top three
#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
@roryokane
roryokane / Runner.scala
Created May 31, 2012 06:30
exploratory FizzBuzz in Scala
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)
}