Skip to content

Instantly share code, notes, and snippets.

View undecided's full-sized avatar

Matthew Bennett-Lovesey undecided

View GitHub Profile
@undecided
undecided / implicit_returns_with_ensure.rb
Created August 24, 2012 09:12
I got caught out by this. Anyone want to guess which string gets returned by this method?
def foo
raise "I iz a fool"
"But you don't know, man"
rescue Exception => e
"I caught your foolness"
ensure
"and I returned you a fish"
end
@undecided
undecided / fisher_yates_shuffle.coffee
Last active January 21, 2016 14:36 — forked from CurtisHumphrey/fisher_yates_shuffle.coffee
Fisher-Yates shuffle (in-place) in coffeescript
###
Randomize array element order in-place.
Using Fisher-Yates shuffle algorithm.
###
Array.prototype.shuffle = ->
for i in [(@length - 1) .. 0]
j = Math.floor(Math.random() * (i + 1))
[@[i], @[j]] = [@[j], @[i]]
@
@undecided
undecided / nil-array.rb
Created December 21, 2013 16:35
Answer to the question: how to initialise a multi-dimensional array, filled with nils, in ruby?
# My answer at the time was to do something like this
[[nil]*3]*4
#=> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
# The next question was whether I knew some initializer to do that for us.
# Apparently, there is:
Array.new(4) { Array.new(3) }
#=> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
# But that wasn't the brainwave my brain came up with just now.
def a(p)
p.call()
raise "The world is safe"
end
def b
x = Proc.new { return Proc.new { return false } }
a(x).call
raise 'Maybe here, we might save the world. Let us do that.'
end
@undecided
undecided / compile_mruby.rb
Created March 6, 2013 09:36
Quick and dirty script for compiling a ruby file with mruby. Run it with ruby ./compile_mruby.rb my_mruby_file.rb
#!/usr/bin/env ruby
require 'fileutils'
def shell(cmd)
puts "Running #{cmd}"
`#{cmd}`
end
infile = ARGV[0]
@undecided
undecided / kitten.rb
Created January 31, 2013 16:55
I asked Joey to fix my code. She refused, but she did write this for me #soProud
class Kitten
def initialize
set_colour
set_name
end
def set_colour
if rand(0..1) == 1
@colour = 'White'
@undecided
undecided / gist:4554774
Last active December 11, 2015 05:49
Load tester
#!/usr/bin/env ruby
require 'net/http'
URL_LIST = [
[:get, 'http://quickwebdesign.net'],
[:post, 'http://retrodude.com']
].map { |action, url| [action, URI.parse(url)] }
POST_DATA = -> do
{ :name => "foo", :what => 'ho', :interesting => rand(999999) }
@undecided
undecided / simple_benchmark.rb
Last active December 11, 2015 05:48
Simple benchmarker
def simple_benchmarker
benchmarks = []
activity = ->(name) { benchmarks << [name, Time.now] }
activity[:started]
yield activity
activity[:finished]
display_benchmarks benchmarks
rescue Exception => e
activity[:error_occurred]
display_benchmarks benchmarks
@undecided
undecided / magical_mr_mistoffelees.rb
Created August 31, 2012 10:36
How do I do this?
# Premise: a chainable DSL where evaluating a section in an if statement calls valid?
# Example: Each item changes the current synonym, it tests it against
class Moggy
SYNONYMS = [:cat, :moggy, :pussycat, :feline]
def set(syn)
@synonym = syn
self
end
@undecided
undecided / not_matches_vs_does_not_match.rb
Created August 28, 2012 13:59
Another ruby funny that caught me out... Want to guess what these should output?
"fish" =~ /ish/
"fish" !=~ /ish/
!("fish" =~ /ish/) # equivalent to "fish" !~ /ish/ - thanks @MrJaba and @kerryb!