Skip to content

Instantly share code, notes, and snippets.

@jimweirich
jimweirich / pullrequest.rb
Created May 5, 2013 00:51
Script I use to pull in pull requests from GitHub
#!/usr/bin/ruby -wKU
if ARGV.empty?
puts "Usage: pullrequest user:branch"
exit 1
end
url = `git config --get remote.origin.url`
unless %r{([^/]+)\.git$} =~ url
puts "Unable to determine repo from url (#{url})"
@jimweirich
jimweirich / expression.rb
Last active December 16, 2015 22:49
Simple example of Parslet.
require 'parslet'
class Expressions < Parslet::Parser
rule(:operator) { match("[+*/-]").as(:op) }
rule(:operation) { operator >> space >> vector.as(:args) }
rule(:space) { match('\s').repeat(1) }
rule(:numbers) { space >> number }
rule(:vector) { (number >> numbers.repeat(1)).as(:vec) }
rule(:number) { match("[0-9]").repeat(1).as(:number) }
rule(:expr) { vector | number | operation }
# in Norway
module Calendar
module Holiday
FEBRUARY = 2
NOVEMBER = 11
def mothersday(year)
second_sunday_in(FEBRUARY, year)
end
@jimweirich
jimweirich / shell.out
Created April 23, 2013 02:43
What's up with line 4 in the exuberant_ctags output?
$ exuberant_ctags -e testdata/ruby/dog.rb
$ cat TAGS
^L
(null),142
module Animal^?Animal^A1,0
class Base^?Base^A5,34
class Dog < Animal::Base^?Dog^A8,54
def speak(string)^?speak^A11,111
def wag^?wag^A15,165
#!/usr/bin/env ruby
# -*- ruby -*-
begin
require 'rubygems'
rescue LoadError
end
exts = ['.rb', '.rhtml', '.erb', '.builder', '.haml', '.css', '.js', '.coffee', '.yml', '.yaml' ]
if ARGV[0] =~ /^(\+|\.[a-zA-Z0-9]+)$/
@jimweirich
jimweirich / nav_option_demo_snippet.rb
Created March 6, 2013 22:16
Some unusual Ruby code I've been working on today.
class NavOptionDemo < NavOption
include CFields
uint32_t :ctrl_state # Flying state (landed, flying,
# hovering, etc.) defined in
# CTRL_STATES enum.
uint32_t :vbat_flying_percentage # battery voltage filtered (mV)
alias :battery_level :vbat_flying_percentage
float32_t :theta # UAV's pitch in milli-degrees
@jimweirich
jimweirich / parse_snippet.rb
Created March 6, 2013 00:26
Sample of parsing large binary buffer one variable length section at a time.
loop do
section = parse_section_from_binary_buffer(data)
break if section.nil?
data = data[section.size .. -1]
end
@jimweirich
jimweirich / mem.output
Last active December 14, 2015 13:28
Test of memory usage when creating a lot of slices of a large array.
# I'm guessing that MRI and JRuby don't copy the array elements
# when making a slice, they just reference the original copy with
# new index bounds.
$ rvm use ruby-1.9.3-p286
Using /Users/jim/.rvm/gems/ruby-1.9.3-p286
$ ruby mem.rb
Before: 12716
After: 12744
@jimweirich
jimweirich / bowling.rb
Last active December 14, 2015 02:58
Bowling kata tests (one version at least)
class Bowling
def score_rolls(rolls)
init_rolls(rolls)
score = 0
frame = 0
while frame < 10
if strike?
score += sum_of_next_rolls(3)
consume(1)
elsif spare?
@jimweirich
jimweirich / roman_numeral_converter.rb
Created February 22, 2013 00:26
Another roman numeral converter solution
class RomanNumeralConverter
CONVERTS = {
"I" => 1,
"V" => 5,
"X" => 10,
"L" => 50,
"C" => 100,
"D" => 500,
"M" => 1000,
}