Skip to content

Instantly share code, notes, and snippets.

View zporter's full-sized avatar
👨‍💻

Zach Porter zporter

👨‍💻
View GitHub Profile
@zporter
zporter / pw_gen.rb
Created June 7, 2011 19:47
Nice little hash / password generator for Ruby 1.9
(('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a).sample(30).join #=> "yh8D5NrognVw16GldXPcCxJ24ALU3W"
# with special characters
(('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a + ['!','@','#','$','%','^','&','*']).sample(30).join #=> "Nc&inVolamjMLWfsE#k6$D^p2!b@XC"
@zporter
zporter / collection_class_name.rb
Created July 20, 2011 18:13
HTML Helper method to return appropriate class name based on iteration in the each loop
def collection_class_name(collection, index)
if index == 0
return 'first'
elsif index == ( collection.size - 1 )
return 'last'
end
end
@zporter
zporter / gist:1153918
Created August 18, 2011 11:53
RVM 1.7 Notes
NOTE: For all installations, as of 1.7, RVM no longer autoloads .rvmrc files. In order to return this
functionality, you MUST add 'export rvm_project_rvmrc=1' to your $HOME/.rvmrc file.
This causes RVM to override 'cd' which, while toggleable even < 1.7, is currently defaulted to 'off'.
This knob returns the previous behaviour to active which causes per-project .rvmrc files to be loaded
once again.
Example: echo 'export rvm_project_rvmrc=1' >> $HOME/.rvmrc && rvm reload
@zporter
zporter / field_error_proc.rb
Created November 9, 2011 15:42
customize the Rails error messages that wrap around form fields
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if html_tag =~ /<label[^>]+/
"<div class='field_with_errors'>#{html_tag}".html_safe
else
"#{html_tag}</div>".html_safe
end
end
@zporter
zporter / parsing.lhs
Created December 3, 2015 19:57
EDX: FP 101 Parser
> module Parsing where
>
>
> import Data.Char
> import Control.Monad
> import Control.Applicative
>
> infixr 5 +++
The monad of parsers
@zporter
zporter / traffic_light.rb
Last active December 27, 2015 11:59
Confident Ruby: Traffic Light Refactoring
class TrafficLight
class Lamp
%w(name color).each do |attr|
define_singleton_method("#{attr}=") do |value|
define_method(attr) { value }
end
end
def to_s
name
@zporter
zporter / rack_deflater.rb
Created June 15, 2012 14:48
Fixes deadlock issue on Rack::Deflater
require 'rack/deflater'
module Rack
class Deflater
class GzipStream
def each(&block)
@writer = block
gzip =::Zlib::GzipWriter.new(self)
gzip.mtime = @mtime
@body.each { |part|
@zporter
zporter / form_helpers.ex
Created March 6, 2017 16:15
Simple Form example for Phoenix
defmodule AppName.FormHelpers do
@doc """
Generates a form input tag
Example:
<%= input f,
:name,
input: [
placeholder: gettext("What should we call you?"),
],
@zporter
zporter / address.ex
Last active May 17, 2017 02:00
Ecto: Embedded Schemas
use Ecto.Schema
import Ecto.Changeset
@type t :: module
# A required field for all embedded documents
@primary_key {:id, :binary_id, autogenerate: true}
schema "" do