Skip to content

Instantly share code, notes, and snippets.

@ryancastro
ryancastro / fast_random_key_gen.rb
Created November 19, 2015 20:55
Random key/token generation that is much faster than UUID (at a higher risk, obviously)
# From:
# https://stackoverflow.com/questions/88311/how-best-to-generate-a-random-string-in-ruby
#Generates a random URL-safe key/token much faster than UUID
#Comes with additional risk for high integrity keys, but for many cases it's good enough.
#I'm using it to generate unique file names without the overhead of UUID.
#Occasionally returns a key of length-1
length = 5
p rand(36**length).to_s(36)
@ryancastro
ryancastro / Disable SSL.rb
Created February 4, 2015 17:47
Disable SSL
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
@ryancastro
ryancastro / easy_string_capture.rb
Last active August 29, 2015 14:14
Easy Ruby Regex - the shortest regex find, and replace methods in Ruby.
#index capture groups:
url = "https://gist.github.com/ryancastro/whatever.git"
p url[/(.*)\/whatever.git/, 1]
p url.gsub(/(.*)\/whatever.git/, '\1')
#named capture groups:
url = "https://gist.github.com/ryancastro/whatever.git"
p url[/(?<url>.*)\/whatever.git/, "url"]
p url.gsub(/(?<url>.*)\/whatever.git/, '\k<url>')
@ryancastro
ryancastro / speak.rb
Created October 24, 2014 14:15
Speaking through windows Voice API
def speak sentence
sentence = sentence.gsub /'/, "''"
`powershell.exe -c $speaker = new-object -com SAPI.SpVoice; $speaker.Speak('#{sentence}');`
end
speak "Hello."
@ryancastro
ryancastro / fb_delete.js
Created February 25, 2014 21:57
Delete all of your Facebook activity. Uses the mobile interface rather than the normal browser interface because this way is much faster and reliable.
// Load Facebook's mobile activity log
// https://m.facebook.com/#{you}/allactivity?log_filter=all
// loads jQuery into Facebook
var jq = document.createElement('script');
jq.src = "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// wait for jQuery to load up.
setTimeout(removeIt, 2000);
@ryancastro
ryancastro / Command line arguments.rb
Created August 28, 2013 21:02
Just a little reminder on an interesting way to split command line arguments via commas.
def stuff(things)
things.each do |command|
puts "I am executing this command: #{command.strip}\n"
end
end
stuff(ARGV.join(" ").split(","))
# cmd: test argument1, argument2, argument 3 things, argument 4
# I am executing this command: argument1
# I am executing this command: argument2
@ryancastro
ryancastro / Sublime text searching.markdown
Created August 1, 2013 13:27
Searching Sublime Text regex across multiple new line characters. Because I keep forgetting.

Regular expressions in Sublime Text 2 only seach single lines by default. You can force them to search all lines with an inline search modifier: (?s)

@ryancastro
ryancastro / window_switcher.rb
Last active December 20, 2015 11:29
Window-switcher: A co-worker of mine asked me to help him alternate windows on their display. Stats for x seconds, mission statement for x seconds. Here's the 5 minute result.
#'compiled' with Ocra
require 'win32ole'
@wsh = WIN32OLE.new('Wscript.Shell')
@title1, @title1_seconds, @title2, @title2_seconds = ARGV
if !@title2_seconds
puts "usage: window1_title seconds window2_title seconds"
puts "ex: Skype 10 Untitled 5"
puts "note: you only need to match the first letters in the window title."
puts "'Untitled' will find the 'Untitled - Notepad' window"
@ryancastro
ryancastro / Sockets example.md
Last active December 20, 2015 09:19
Using Ruby sockets, because the examples in the documentation don't work at all. Simple client/server messaging example.

Here's a little tutorial in Ruby sockets.

@ryancastro
ryancastro / test a number in Ruby.rb
Created July 25, 2013 15:45 — forked from anonymous/test a number in Ruby
Test to see if a number is a number in Ruby. Useful because: "I am a string".to_i returns 0, which is an an Integer.... so my string will test positive for a number.
def isInt?(number)
true if Integer(number) rescue false
end
isInt?("13")
result = true if Integer("12") rescue false # true
result = true if Integer("test") rescue false #false
result = true if Float("test") rescue false #false
result = true if Float(13.2) rescue false #true