Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View presidentbeef's full-sized avatar
🐢
May be slow to respond on OSS projects

Justin Collins presidentbeef

🐢
May be slow to respond on OSS projects
View GitHub Profile
@presidentbeef
presidentbeef / README.md
Last active September 1, 2020 16:29
Access Rails routes programmatically

RouteReader

@presidentbeef
presidentbeef / dropbox_password.rb
Last active August 30, 2020 11:31
Dropbox-style Password Storage
# Based on https://blogs.dropbox.com/tech/2016/09/how-dropbox-securely-stores-your-passwords/
require 'bcrypt' # bcrypt gem
require 'digest/sha2'
require 'openssl'
# Generate an encrypted hash from a plaintext password,
# given an AES key and AES IV.
def store(password, key, iv)
# Hash password to get good 512 bits
# because bcrypt only uses the first 72 bytes.
@presidentbeef
presidentbeef / kmuddy_on_mageia_5.md
Created December 12, 2016 01:47
Kmuddy on Mageia 5

Keybase proof

I hereby claim:

  • I am presidentbeef on github.
  • I am presidentbeef (https://keybase.io/presidentbeef) on keybase.
  • I have a public key whose fingerprint is 56C5 4454 50FF 9138 7CC6 6C0D A6A5 CB66 1402 CC6A

To claim this, I am signing this object:

@presidentbeef
presidentbeef / unsorted.rb
Created January 12, 2014 01:46
Count how unsorted an array is by number of swaps required to sort it.
def count_swaps list, &sort_block
swaps = 0
# Get a sorted list for comparison
sorted = list.sort &sort_block
# Check each elements against where they should be,
# swapping them if necessary and counting the swaps.
list.each_with_index do |element, index|
next if element == sorted[index]
@presidentbeef
presidentbeef / intel_7260.md
Created August 5, 2015 17:45
Intel Corporation Wireless 7260 on Mageia 5 (but with kernel-tmb-laptop)

After updating from Mageia 4 to 5, the builtin wireless on my Lenovo T440s stopped working. When attempting to configure the card via the network center or drakconnect, I would get the message "Unable to find network interface for selected device (using iwlwifi driver)". Removing and re-adding the iwlwifi module via modprobe did nothing. The wireless card was definitely available according to lspci. I had the iwlwifi-agn-ucode package installed. ls /lib/firmare listed several ucode files for iwlwifi-7260.

Some investigation found Mageia 5 does not provide a kernel-tmb-laptop package, which means I was still running on 3.14.43-tmb-laptop-1.mga4. Changing the kernel just to go from 3.14 to 3.19 but perhaps lose laptop features didn't seem worth it.

To get the wireless working again, I just needed to go to https://wireless.wiki.kernel.org/en/users/Drivers/iwlwifi download the correct ucode file (iwlwifi-7260-ucode-25.228.9.0.tgz) which contains iwlwifi-7260-9.ucode. Copy to /lib/firmare, rest

#!/usr/bin/ruby -w
require 'set'
Entry = Struct.new :id, :instance do
def self.parse(line)
if /ID=\s*'([^']*)'\s+INSTANCE=\s*'([^']*)'/ =~ line
new $1, $2
else
raise "Cannot parse: %p" % line
end
@presidentbeef
presidentbeef / String#html_safe
Last active December 14, 2015 00:48
String#html_safe does not make a string HTML safe.
Loading development environment (Rails 3.2.11)
1.9.3p374 :001 > s = "<script>alert('hello')</script>"
=> "<script>alert('hello')</script>"
1.9.3p374 :002 > ERB::Util.html_escape s
=> "&lt;script&gt;alert(&#x27;hello&#x27;)&lt;/script&gt;"
1.9.3p374 :003 > safe = s.html_safe
=> "<script>alert('hello')</script>"
1.9.3p374 :004 > ERB::Util.html_escape safe
=> "<script>alert('hello')</script>"
@presidentbeef
presidentbeef / conversions.rb
Created January 10, 2013 04:42
Simple way to disable XML parsing of symbol/YAML types
#activesupport/lib/active_support/core_ext/hash/conversions.rb
unless defined?(XML_PARSING)
XML_PARSING = {
- "symbol" => Proc.new { |symbol| symbol.to_sym },
+ "symbol" => Proc.new { |symbol| symbol.to_s },
"date" => Proc.new { |date| ::Date.parse(date) },
"datetime" => Proc.new { |time| ::Time.parse(time).utc rescue ::DateTime.parse(time).utc },
"integer" => Proc.new { |integer| integer.to_i },
@@ -76,7 +76,7 @@ module ActiveSupport #:nodoc:
"decimal" => Proc.new { |number| BigDecimal(number) },
@presidentbeef
presidentbeef / gist:2720022
Created May 17, 2012 16:33
Show ruby_parser output on command line
#!/usr/bin/env ruby
require 'rubygems'
require 'ruby_parser'
require 'pp'
pp RubyParser.new.parse ARGV[0]