Skip to content

Instantly share code, notes, and snippets.

View moxley's full-sized avatar

Moxley Stratton moxley

View GitHub Profile
@moxley
moxley / gfr.rb
Created August 30, 2012 21:43 — forked from sbecker/gfr.rb
Global find and replace with optional confirm (with colored diff)
#! /usr/bin/env ruby
# Save this file to somewhere in your PATH, like ~/bin/gfr, and chmod it with: chmod u+x gfr
require 'rubygems'
require "highline/system_extensions"
require 'colorize'
require 'optparse'
require 'ostruct'
include HighLine::SystemExtensions
@moxley
moxley / sample.css
Created September 17, 2012 22:24
CSS Critique
/*
* CSS Critique - How many issues can you find in this stylesheet?
*/
.blue-black-select {
background-color: white;
}
#background {
width:100%;
@moxley
moxley / map_hash.rb
Created September 18, 2012 22:23
Map Enumerable to a Hash instead of an Array
module Enumerable
def map_hash(memo = nil, &block)
i = 0
inject(memo || {}) do |hash, item|
if kind_of?(Hash)
key = item.first
value = item.last
else
key = i
value = item
@moxley
moxley / my_class.rb
Created September 20, 2012 00:13
Weird Ruby Behavior
class MyClass
def value
"VALUE!"
end
def do_something
puts value # Outputs "VALUE!"
if false
value = nil # Should not execute
end
@moxley
moxley / file_string_replacer.rb
Created April 16, 2013 17:35
Prototype string replacer that parses the input character-by-character. Final implementation to be written in Go.
# To test, create a text file called test.txt in the current directory with the following content:
#
# Hello foo...
#
# Then run: ruby file_string_replacer.rb
# It will output send the modified version of the file contents to STDOUT
class FileStringReplacer
attr_accessor :file_path, :char
def validate_colors
colors = %w(color_title color_title2 color_title3 color_title4)
as_str = colors.map {|c| send(c).present? : 'p' : ' '}
trimmed = as_str.trim
if trimmed =~ / /
errors[:base] << "Blank color found before non-black color"
end
end
@moxley
moxley / to_lua.rb
Last active January 4, 2016 10:09
Convert Ruby data to formatted Lua code
# Convert Ruby data to formatted Lua code
# irb(main):001:0> load 'to_lua.rb'; puts ToLua.format(['what', "she's", nil, false, true, ['yep'], [], {}, {foo: 'foo'}, {'bar' => 'bar'}, {0 => 'zero'}])
# {'what', 'she\'s', nil, false, true, {'yep'}, {}, {}, {foo = 'foo'}, {['bar'] = 'bar'}, {[0] = 'zero'}}
module ToLua
extend self
# Format value
def format(v)
if v.kind_of?(Array)
format_array(v)
@moxley
moxley / pullreq
Last active August 29, 2015 13:57
Create Pull Request from current branch
#!/usr/bin/env ruby
# Open Pull Request for current branch
# Works in OS X
require 'ostruct'
branch=`git branch | grep '\*' | sed 's/\* //'`
remote_rows = `git remote -v | grep push`.split("\n").map do |line|
match = line.match(/^([^\s]+)\s+([^\s]+)/)
@moxley
moxley / gist:9620715
Last active August 29, 2015 13:57
Markdown lists with embedded, language-specific code blocks

List items:

  1. List item 1

    def load_value(value)
      loader = Loader.new
      loader.load(value)
    end
@moxley
moxley / gist:10689371
Last active August 29, 2015 13:59
Struct-like objects in Ruby

Struct-Like Objects

  • Inspired by C's struct
  • Situations where Hashes are used
  • Why do we use hashes?
    • Group key-value data together under one value
  • Why don't we choose normal objects instead?
    • Needs explicit class definition (false)
    • Need place for class to live (false)
  • When to use hashes over struct-like objects