Skip to content

Instantly share code, notes, and snippets.

View pvdb's full-sized avatar
🖥️
Making computers do things!

Peter Vandenberk pvdb

🖥️
Making computers do things!
View GitHub Profile
@pvdb
pvdb / determine_home.rb
Created June 11, 2014 13:40
Get current user's home directory, even if ${HOME} isn't set in env
# as a String
HOME = ENV['HOME'] || File.expand_path("~#{Etc.getlogin}")
# as a Pathname
HOME = Pathname.new(ENV['HOME'] || File.expand_path("~#{Etc.getlogin}"))
@pvdb
pvdb / proxy.rb
Last active August 29, 2015 14:24 — forked from torsten/proxy.rb
#!/usr/bin/env ruby
# A quick and dirty implementation of an HTTP proxy server in Ruby
# because I did not want to install anything.
#
# Copyright (C) 2009-2014 Torsten Becker <torsten.becker@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
#!/usr/bin/env ruby
require 'pp'
def ruby_repl processor = nil
unless (processor && Proc === processor) || block_given?
warn "Please invoke `ruby_repl` with a block or a proc..."
else
in_pry = Kernel.const_defined?("Pry::ColorPrinter")
cmd_count = 0 # for use in the command prompt
@pvdb
pvdb / process_tcp.rb
Last active August 29, 2015 14:25
Get the number of TCP connections owned by current Ruby process
#
# This first version should work on Mac OS X and Linux, but it spawns a process
#
# the number of TCP connections owned by the process.
def Process.tcp_count() `lsof -l -n -P -p #{Process.pid} -a -i tcp | egrep -v '^COMMAND' | wc -l`.chomp.to_i ; end
private
def mediainfo!
raw_response = `#{@mediainfo_binary} #{@escaped_full_filename}`
raise "Execution of #{mediainfo_binary} failed!" unless $? == 0
return raw_response
end
>> require 'mediainfo'
=> true
>> mediainfo = Mediainfo.new "foo(bar).mov"
private
def mediainfo!
# ANSI-C quoting: http://www.faqs.org/docs/bashman/bashref_12.html
@escaped_full_filename = @full_filename.gsub(/\\|'/) { |c| "\\#{c}" }
raw_response = `#{@mediainfo_binary} $'#{@escaped_full_filename}'`
raise "Execution of #{mediainfo_binary} failed!" unless $? == 0
return raw_response
end
>> require 'mediainfo'
private
def mediainfo!
`#{@mediainfo_binary} #{@full_filename}`
end
>> require 'mediainfo'
=> true
>> mediainfo = Mediainfo.new "foo(bar).mov"
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `mediainfo foo(bar).mov'
# question asked on aardvark.com: http://vark.com/t/b27d66
# Is there a ruby function to find the shortest and longest string inside an array?
# (my array will only store a bunch of stirngs)
# Here's a snippet of Ruby code that should get you going...
>> %w{ foo bar blegga very-long-string }.sort { |a, b| a.length <=> b.length }.first
=> "foo"
>> %w{ foo bar blegga very-long-string }.sort { |a, b| a.length <=> b.length }.last
=> "very-long-string"
#
# e.g. embedded tab "\t" and newline "\n" characters
#
>> puts 'first\tsecond\tthird'
first\tsecond\tthird
=> nil
>> puts "first\tsecond\tthird"
first second third
=> nil
#
# ActiveRecord SQL Logging on the Rails Console
# ---------------------------------------------
#
# Various blog posts out there show you how to log all SQL statements
# executed by ActiveRecord to STDOUT when you're in a Rails console:
#
# * http://robots.thoughtbot.com/post/159806033/irb-script-console-tips
# * http://weblog.jamisbuck.org/2007/1/8/watching-activerecord-do-it-s-thing
# * http://toolmantim.com/thoughts/system_wide_script_console_logging