Skip to content

Instantly share code, notes, and snippets.


Matthew Bennett-Lovesey undecided

View GitHub Profile
undecided / ruby_and_mirah.rb
Created Mar 9, 2011
Shows a simple class implemented in Ruby and Mirah
View ruby_and_mirah.rb
# Ruby
class Rubyish
def a_method(some_string, some_hash)
@message = some_string
@transform = some_hash
puts @message
def another_method
@transform.each_pair do |key, val|
undecided / implicit_returns_with_ensure.rb
Created Aug 24, 2012
I got caught out by this. Anyone want to guess which string gets returned by this method?
View implicit_returns_with_ensure.rb
def foo
raise "I iz a fool"
"But you don't know, man"
rescue Exception => e
"I caught your foolness"
"and I returned you a fish"
undecided / not_matches_vs_does_not_match.rb
Created Aug 28, 2012
Another ruby funny that caught me out... Want to guess what these should output?
View not_matches_vs_does_not_match.rb
"fish" =~ /ish/
"fish" !=~ /ish/
!("fish" =~ /ish/) # equivalent to "fish" !~ /ish/ - thanks @MrJaba and @kerryb!
View magical_mr_mistoffelees.rb
# 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
undecided / simple_benchmark.rb
Last active Dec 11, 2015
Simple benchmarker
View simple_benchmark.rb
def simple_benchmarker
benchmarks = []
activity = ->(name) { benchmarks << [name,] }
yield activity
display_benchmarks benchmarks
rescue Exception => e
display_benchmarks benchmarks
View gist:4554774
#!/usr/bin/env ruby
require 'net/http'
[:get, ''],
[:post, '']
].map { |action, url| [action, URI.parse(url)] }
POST_DATA = -> do
{ :name => "foo", :what => 'ho', :interesting => rand(999999) }
undecided / kitten.rb
Created Jan 31, 2013
I asked Joey to fix my code. She refused, but she did write this for me #soProud
View kitten.rb
class Kitten
def initialize
def set_colour
if rand(0..1) == 1
@colour = 'White'
undecided / compile_mruby.rb
Created Mar 6, 2013
Quick and dirty script for compiling a ruby file with mruby. Run it with ruby ./compile_mruby.rb my_mruby_file.rb
View compile_mruby.rb
#!/usr/bin/env ruby
require 'fileutils'
def shell(cmd)
puts "Running #{cmd}"
infile = ARGV[0]
View crazy_ruby_procs.rb
def a(p)
raise "The world is safe"
def b
x = { return { return false } }
raise 'Maybe here, we might save the world. Let us do that.'
undecided / nil-array.rb
Created Dec 21, 2013
Answer to the question: how to initialise a multi-dimensional array, filled with nils, in ruby?
View nil-array.rb
# My answer at the time was to do something like this
#=> [[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: { }
#=> [[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.
You can’t perform that action at this time.