Skip to content

Instantly share code, notes, and snippets.

@granolocks
granolocks / object_sub_constants.rb
Created March 10, 2012 01:16
Enumerate Constants in object space
class Object
def self.sub_constants(prefix = "", namespace=[])
self.constants.each do |constant|
constant = constant.to_s
puts prefix + constant
#puts eval("#{constant}.methods").inspect
begin
next if %w{ Object Module Class }.include?(constant) || namespace.include?(constant)
b = (namespace.dup) << constant
eval_string = b.join('::')
@granolocks
granolocks / extend_or_include.rb
Created March 13, 2012 13:02
Difference between include and extend for loading of a module.
module Mojule
def dynamic_method
if self.class.eql?(Class)
puts "I am now a class method of #{self.name}"
else
puts "I am now an instance method of #{self.class}"
end
end
end
@granolocks
granolocks / tshark_grep.sh
Created March 13, 2012 13:18
Use Tshark and Grep to Inspect for a custom Header Key
# Use tshark to grep custom header key value (HEADER_KEY) out of responses from a local rails server
tshark -i eth0 -d tcp.port==3000,http tcp port 3000 -V -R "http.response" | grep HEADER_KEY -B 10 -A 10
@granolocks
granolocks / enum_loop.rb
Created March 13, 2012 13:22
Loop with Enumerator
# Enumerator to loop with
months = (1..12).to_a.reverse.each
# Integer to loop with
year = 1000
# month counts down, then resets through loop
# year goes up to 2014 then ends
until year == 2014 do
begin
@granolocks
granolocks / app_helpers_my_model_validator.rb
Last active October 1, 2015 23:47
Custom Rails Validator
# app/helpers/my_model_helper.rb
class MyModelValidator < ActiveModel::Validator
def validate(mymodel)
unless mymodel.starts_at.nil? || mymodel.ends_at.nil?
validate_times(mymodel)
end
end
def validate_times(mymodel)
@granolocks
granolocks / rspec_mappings.vim
Created March 20, 2012 19:50
Useful Keymappings
" bind \f to run rspec on current file
:map \f :w\|!rspec %<cr>
" bind \l to run rspec on current line
:map \l :w\|:exe "!rspec % -l " . line('.')<cr>
" bind \w to run rspec on current file
" this one kind of sucks...
:map \w :w\|:exe "!rspec % -e " . expand("<cword>")<cr>
# helps you play twister...
# yup....
class Twister
def colors
%w{ red blue yellow green }
end
def left_right
@granolocks
granolocks / count_ruby_lines.sh
Created March 22, 2012 20:10
How Many Lines of Ruby in a New Rails app?
#!/usr/bin/bash
# counts all lines in all ruby files in a directory
find . -type f -name *.rb -exec cat {} \; | grep -vE "^([[:space:]]+)?\#" | grep -vE "^([[:space:]]+)?$" | wc -l
@granolocks
granolocks / simple_serial.rb
Created March 27, 2012 17:38
Simple Serial
# Simple Serial Listener I have used to listen to Arduino from Ubuntu.
# That is all.
require "serialport"
#params for serial port
port_str = "/dev/ttyACM0" #may be different for on different machines
baud_rate = 9600
data_bits = 8
stop_bits = 1
@granolocks
granolocks / gist:2223850
Created March 28, 2012 05:16 — forked from JosephPecoraro/shell-execution.rb
Shell Execution in Ruby
# Ways to execute a shell script in Ruby
# Example Script - Joseph Pecoraro
cmd = "echo 'hi'" # Sample string that can be used
# 1. Kernel#` - commonly called backticks - `cmd`
# This is like many other languages, including bash, PHP, and Perl
# Returns the result of the shell command
# Docs: http://ruby-doc.org/core/classes/Kernel.html#M001111