Skip to content

Instantly share code, notes, and snippets.

View domhnall's full-sized avatar

Domhnall Murphy domhnall

View GitHub Profile
@domhnall
domhnall / pre-commit
Last active November 30, 2022 17:02
Git pre-commit hook for Rails to highlight route changes
#!/bin/sh
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
@domhnall
domhnall / operator_precedence_quiz.rb
Last active November 7, 2022 22:04
Short quiz to test how well you know operator precedence rules in ruby. See the [blog post](https://www.vector-logic.com/blog/posts/test-yourself-on-operator-precdence-in-ruby) to have a go at the quiz
# To test yourself on the actual quiz go to
# https://www.vector-logic.com/blog/posts/test-yourself-on-operator-precdence-in-ruby
a = false
b = true
c = 3
d = 4
expressions = [
"1 + 2 * 2", # Outputs 5
@domhnall
domhnall / ruby_object_model_quiz.rb
Last active November 20, 2022 21:36
Short quiz to test how well you know the ruby object model. See the [blog post](https://www.vector-logic.com/blog/posts/test-yourself-on-ruby-object-model) to have a go at the quiz
# To test yourself on the actual quiz, see
# https://www.vector-logic.com/blog/posts/test-yourself-on-ruby-object-model
puts "Question 1"
class First; end
f = First.new
puts f.class.superclass # Object
puts "\n\n"
puts "Question 2"
@domhnall
domhnall / ruby-arrays-quiz.rb
Created February 21, 2023 13:49
Short quiz to test how well you know ruby array methods. See the [blog post](https://www.vector-logic.com/blog/posts/test-yourself-on-ruby-array-operations) to have a go at the quiz
# To test yourself on the actual quiz, see
# https://www.vector-logic.com/blog/posts/test-yourself-on-ruby-array-operations
def print_question_number(n)
puts ["="*20, n, "="*20].join(" ")
end
# Array indexing options
print_question_number(1)
arr = ['a', 'b', 'c', 'd', 'e']
@domhnall
domhnall / ensure_return_value.rb
Created March 15, 2023 21:32
Snippet demonstrating that there is no implicit return value from the ensure block in ruby
def no_ensure
:ok
end
def with_ensure
:ok
ensure
:ensure
end
@domhnall
domhnall / combinatoins.rb
Created March 23, 2023 21:30
Small script demonstrating how we can generate combinations from arrays of arrays
def pretty_print(array_of_arrays)
array_of_arrays.each do |array|
puts "[#{array.join(",")}]"
end
end
def combinations_for_free(array_of_arrays)
array_of_arrays[0].product(*array_of_arrays[1...])
end