Skip to content

Instantly share code, notes, and snippets.

@PandaWhisperer
PandaWhisperer / attempt.rb
Created August 18, 2020 00:18
Attempt n times
# Attempt a certain action several times before giving up
#
# Usage: `attempt(3.times, rescue: RuntimeError) do ... end`
#
def attempt(times, options = {})
times = times.is_a?(Enumerator) ? times.max : times.to_i
begin
attempt ||= 0
puts "Checking (attempt #{attempt+1})..."
require 'vessel'
class AdCrawler < Vessel::Cargo
GAM_GET_SLOTS_JS = <<~JS.gsub(/\n/, '')
googletag.pubads().getSlots().map(slot => ({
adUnitPath: slot.getAdUnitPath(),
slotElementId: slot.getSlotElementId(),
sizes: slot.getSizes().map(size =>
typeof size == 'string' ? [0, 0] : [size.getWidth(), size.getHeight()])
}))
require 'capybara'
require 'capybara/apparition'
require 'forwardable'
Capybara.threadsafe = true
Capybara.register_driver :apparition do |app|
Capybara::Apparition::Driver.new(app, headless: true,
js_errors: false,
browser_logger: nil)
#/usr/bin/env bash
# MIT © Sindre Sorhus - sindresorhus.com
# git hook to run a command after `git pull` if a specified file was changed
# Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
def countwovels(str)
for vowel in 'aeiouAEIOU'.chars
count = str.count(vowel)
if count == 1
puts "#{vowel} appears #{count} time."
elsif count >= 2
puts "#{vowel} appears #{count} times."
end
end
end

JS: Celsius to Fahrenheit (with tests)

Solving the Firehose Javascript challenge Celsius to Fahrenheit with tests.

Introduction

Before we begin, there is a piece of information missing from the challenge. When you run npm install --save readline-sync, you'll likely see an error like this:

class Hello
def greeting(name)
"Hello, #{name}!"
end
end
module Anagram
def self.match(base, candidates)
base_fingerprint = fingerprint(base)
candidates.select { |c| fingerprint(c) == base_fingerprint }
end
private
def self.fingerprint(string)
string.downcase.chars.sort
defmodule Johnann do
def john(n) when n >= 1 do
john_and_ann(n)[:john] |> Map.values
end
def ann(n) when n >= 1 do
john_and_ann(n)[:ann] |> Map.values
end
def john(n)
john_and_ann(n)[:john]
end
def ann(n)
john_and_ann(n)[:ann]
end
def sum_john(n)
john(n).reduce(:+)