Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrew Faraday AJFaraday

View GitHub Profile
@AJFaraday
AJFaraday / dec_bin.rb
Created Oct 1, 2020
The search for a Deciam Binary Two Bit Number™️
View dec_bin.rb
# Two bit numbers
require 'active_support'
def has_two_bits?(s)
r = false
b = 0
c = s.chars.sort
c[-3] != '1' && c[-2] == '1'
end
@AJFaraday
AJFaraday / ultimate_monkey_patch.rb
Created Sep 28, 2020
Possibly the best monkey patch I've ever written
View ultimate_monkey_patch.rb
class NilClass
def method_missing(m, *args, &block)
if [String, Integer, Float, Time].any?{|kls| kls.method_defined?(m)}
nil
else
super
end
end
end
@AJFaraday
AJFaraday / challenge.txt
Last active Apr 8, 2020
Easter Bunny challenge
View challenge.txt
Easter Bunny Hunting:
setup
A square grid 99 x 99
4 egg hunters, one at each corner.
Easter bunny in the middle (it has 100 eggs)
Hunters:
They are run by an array of functions
@AJFaraday
AJFaraday / notap.rb
Created Apr 2, 2020
Overuse of tap? - which is better?
View notap.rb
class SomeClass
def do_things
thing_one
thing_two
thing_three
end
def thing_one
do_something
View gist:9fdd8f6f879a28873cb87ec3fef449c6
# Test out a complex relation in the Rails console
t = Team.find(10004)
t.class.has_one :file, class_name: 'ProjectAsset', foreign_key: :parent_id, primary_key: :project_element_id
t.file
View bot.js
if(api.turn() == 0) {
api.towardsX()
} else if (api.turn() == 1) {
api.towardsY()
} else {
var dirs = ['awayX', 'awayY', 'towardsX', 'towardsY']
if (typeof api.dir == 'undefined') {
api.dir = 0;
}
if ((api.turn() + 1) % 2) {
View bot.js
// grabbed from gist
if((api.turn() % 10) == 0) {
directions = ['towardsY', 'towardsX', 'awayY', 'awayX'];
dir = Math.floor(Math.random() * 4);
api[directions[dir]]();
}
@AJFaraday
AJFaraday / wat.rb
Created Aug 14, 2019
Private methods WAT
View wat.rb
class Wat
def fine
foo
end
def not_fine
self.foo
end
View result.txt
decimal binary
1 000000000 + + + + + + + + = = £0.00
2 000000001 + + + + + + + + 1 = 1 = £0.01
3 000000010 + + + + + + + 2 + = 2 = £0.02
4 000000011 + + + + + + + 2 + 1 = 3 = £0.03
5 000000100 + + + + + + 5 + + = 5 = £0.05
6 000000101 + + + + + + 5 + + 1 = 6 = £0.06
7 000000110 + + + + + + 5 + 2 + = 7 = £0.07
8 000000111 + + + + + + 5 + 2 + 1 = 8 = £0.08
9 000001000 + + + + + 10 + + + = 10 = £0.10
View bot.js
if ((api.turn() % 2) == 0) {
api.towardsY();
} else {
api.towardsX();
}
You can’t perform that action at this time.