Skip to content

Instantly share code, notes, and snippets.

View emdeeeks's full-sized avatar

Gareth Griffiths emdeeeks

View GitHub Profile
@acook
acook / keypress.rb
Created December 2, 2012 18:42
Read keypresses from user in terminal, including arrow keys using pure Ruby. This has since been folded into a much more robust gem called Remedy. https://rubygems.org/gems/remedy & https://github.com/acook/remedy
require 'io/console'
# Reads keypresses from the user including 2 and 3 escape character sequences.
def read_char
STDIN.echo = false
STDIN.raw!
input = STDIN.getc.chr
if input == "\e" then
input << STDIN.read_nonblock(3) rescue nil
@acook
acook / termsize.rb
Created December 2, 2012 17:31
Getting the terminal size in Ruby
# via http://www.megasolutions.net/ruby/Getting-the-size-of-the-terminal-in-a-portable-way-26006.aspx
TIOCGWINSZ = 0x40087468
def get_winsize
str = [0, 0, 0, 0].pack('SSSS')
if STDIN.ioctl(TIOCGWINSZ, str) >= 0
rows, cols, xpixels, ypixels = str.unpack("SSSS")
p rows, cols, xpixels, ypixels
else
puts "Unable to get window size"
@cabron
cabron / flags.rb
Created October 26, 2012 12:15
Ruby: flag printer
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
COLOR_SOURCE_VALUES = 256
COLOR_TARGET_VALUES = 5
COLOR_DIVIDE = COLOR_SOURCE_VALUES / COLOR_TARGET_VALUES
TERM_COLOR_BASE = 16
def rgb_to_xterm(r, g, b) # part of grosser.it/2011/09/16/ruby-converting-html-colors-24bit-to-xtermterminal-colors/
@bct
bct / gist:3913085
Created October 18, 2012 16:37
shell functions for lazy ruby developers
function lsgems() {
rubyversion=$(ruby -v | cut -f2 -d' ' | sed 's/p/-p/')
gemroot=~/.rvm/gems/ruby-"$rubyversion"/gems
ls "$gemroot"
}
function cdgem() {
rubyversion=$(ruby -v | cut -f2 -d' ' | sed 's/p/-p/')
gemroot=~/.rvm/gems/ruby-"$rubyversion"/gems
@mweppler
mweppler / emailer.rb
Created September 14, 2012 07:13
A ruby emailer
# http://ruby-doc.org/stdlib-1.9.3/libdoc/net/smtp/rdoc/Net/SMTP.html
require 'digest/md5'
require 'mime/types'
require 'net/smtp'
require 'optparse'
require 'ostruct'
require 'yaml'
class Emailer
@nthomson
nthomson / parse_email
Created September 5, 2012 04:07
Simple email parser in ruby
#!/usr/bin/ruby
#Returns an email dictionary that includes all of an emails headers as well as each part of the email
#You can reference which part of the email you'd like by its content type
#Ex: email['parts']['text/html'] will get you the text/html version of the email body
#You can reference headers by the header name
#Ex: email['headers']['To'] will return the value of the "to" field
def headers_and_parts(file_path)
headers = {}
file = File.new(file_path, "r")
@mcrmfc
mcrmfc / ruby_vars.rb
Created April 29, 2012 22:09
variables in ruby
class A
@@class_var = 'aclassvar'
@class_instance_var = 'aclassinstancevar'
def initialize
@instance_var = 'ainstance_var'
end
def A.class_method
@kulesa
kulesa / gist:2507892
Created April 27, 2012 09:44 — forked from dhh/gist:2492118
nested routes for namespaces
class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes", "#{@scope[:shallow_prefix]}", "#{routes_name}.rb")))
end
end
BCX::Application.routes.draw do
draw :projects # => config/routes/projects.rb
namespace :admin do
@Jared-Prime
Jared-Prime / gist:2423065
Created April 19, 2012 18:57
Converting RGB to HEX in Ruby
# Beginning Rubyists: simply run >> ruby hex-convertions.rb to see examples
# or use irb to build the code on the fly
# you can convert HEX to DEC using the method .to_i(base),
# which converts a string to a decimal number from the specified base number
puts "00".to_i(16)
puts "FF".to_i(16)
@mattsgarrison
mattsgarrison / Rakefile
Created April 9, 2012 13:57
Rake task to link up my Dotfiles repo.
require 'pathname'
require 'fileutils'
#This assumes this script is your home directory level Rakefile
namespace :dotfiles do
desc "Link ALL the dotfiles!"
task :link do
path = Pathname.new("#{__FILE__}").dirname
Dir.glob "#{path}/Dotfiles/*" do |f|