Skip to content

Instantly share code, notes, and snippets.

class DumbSearch
def initialize(min, max)
#@min = min
#@max = max
@guessed = false
@next_guess = 50
end
attr_reader :next_guess, :guessed
@AJFaraday
AJFaraday / rotate.rb
Created February 21, 2018 12:42
vowel rotation
s = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way – in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only."
vowels = %w{a e i o u}
vowels.each_with_index do |vowel, index|
s.gsub!(vowel,"SUB#{index}SUB")
end
vowels.each_with_index do |vowel, index|
s.gsub!("SUB#{index}SUB", vowels[(index+1)%vowels.length])
end
@AJFaraday
AJFaraday / LETTER.MD
Created August 15, 2017 21:02
Brighton Test Actually - Join In
#Dear Test Actually attendees
My talk at Test Actually (on the 16th of August, 2017), will include some live bug fixing.
I already have an app which is rich in bugs and glitches, but no bug reports to work from.
You can help!
Feel free to find an issue in my app, a simple inventory tracker for a library, which may be used in the talk.
(You will need a GitHub account)
@AJFaraday
AJFaraday / baffling.rb
Last active July 24, 2017 09:55
Unexpected ruby behaviour
# found with ruby 2.2.2 (but I think it must have worked on 1.9, too.
def meth(arg)
argument.is_a?(String) ? 'a' : 'b'
end
meth('foo')
# NameError: undefined local variable or method `argument' for main:Object
# This is completely expected, I've never defined `argument`.
@AJFaraday
AJFaraday / wat.rb
Created July 6, 2017 08:13
Unexpected Else (ruby)
def meth
if false
puts 'not here'
end
else
puts 'WAT'
end
# warning: else without rescue is useless
meth
> u.memberships.any?
# (2.9ms) SELECT COUNT(*) FROM "MEMBERSHIPS" WHERE "MEMBERSHIPS"."USER_ID" = 10080
# => true
> u.memberships.none?
# Membership Load (2.4ms) SELECT "MEMBERSHIPS".* FROM "MEMBERSHIPS" WHERE "MEMBERSHIPS"."USER_ID" = 10080 ORDER BY team_id
# Team Load (6.0ms) SELECT "TEAMS".* FROM "TEAMS" WHERE "TEAMS"."ID" IN (1)
# => false
# The any? method is counting the number of memberships, then presumably asking in Ruby if the number is more than 1.
class Foo
def my_method
puts 'original'
end
end
module Bar
def my_method
puts 'overridden'
end
end
# How can this line throw an error?
@folder = object.respond_to?(:project_element) ? object.project_element : nil
# The error is this:
# undefined method `project_element' for #<Report::Event:0x007fbb6eb8ca78>
# Surely This should be protected, right?
# -----------------------------
require 'rspec'
describe 'something' do
it 'should know what valid means' do
record = Object.new
record.should be_valid?
# undefined method 'valid??' for #<Object#0x0000000f453d063>
end
end
@AJFaraday
AJFaraday / bad_idea.js
Created April 20, 2017 10:20
Don't do this
$('body').on(
'click',
'a#my_element',
function(e) {
e.preventDefault();
$('body').on(
'click',
'a#my_element',
function(e) {
e.preventDefault();