Skip to content

Instantly share code, notes, and snippets.

View rubiety's full-sized avatar

Ben Hughes rubiety

View GitHub Profile
# Don't like for's?
#
module RubyScribe
module Transformers
class Eachifier < Transformer
def transform(e)
if e.is_a?(Sexp) && e.kind == :for
transform_for_to_each(e)
else
super
#!/usr/bin/ruby
## JetBlue AYCJ Notifier
# So JetBlue doesn't want to tell us exactly when AYCJ-ers can book flights.
# This script polls my inbox for any new e-mail from JetBlue and continually
# contacts http://www.jetblue.com/aycj and sees if the "come back tomorrow" image is still there.
# If anything has changed or an error occurs, use's Mac OS "say" to dictate text and wake me up
# so I can book my damn flights. I really wonder how many people are going to stay up tonight hitting the
# refresh button every 10 seconds.
## Concatenates a subset of columns in Excel amongst multiple sheets into one dump
require "rubygems"
require "active_support"
require "roo"
require "faster_csv"
COLUMNS = ["A", "B"] # Column names of whatever you want
[].tap do |o|
### Don't you wish you could use this if-nil idiom?
first_name || "Joe"
# Well you can't if you expect first_name to be blank instead of nil, so Ruby will short-circuit.
# Enter deblank:
class Object
def deblank
self.blank? ? nil : self
end
### Don't you wish you could use this if-nil idiom?
first_name || "Joe"
# Well you can't if you expect first_name to be blank instead of nil, so Ruby will short-circuit.
# Enter deblank:
class Object
def deblank
self.blank? ? nil : self
end
module ApplicationHelper
def tree_options_from_collection_for_select(objects, value = :id, name = :name, selected = nil, options = {})
options[:indent_with] ||= '- '
options[:level] ||= 0
objects.inject([]) do |entries, object|
entries << [options[:indent_with] * options[:level] + object.send(name), object.send(value)]
entries += tree_options_from_collection_for_select(object.children, value, name, selected, options.merge(:level => options[:level] + 1))
entries
end
# Bad:
1.upto(10) do |i|
puts "X" * i
end
# Good:
((nil.object_id - 3)..(2**3 + 2)).map do |i|
lambda { "X" * i }
end.collect {|i| i.send(:call)}.tap do |a|
puts a.class.instance_method(:join).send(:bind, a).call("\n")
indigosun:vim-fu bhughes$ rake preinstall
(in /Users/bhughes/Workspace/Source/vim-fu)
Usage: /usr/local/git/bin/git-submodule [--quiet] [--cached] [add <repo> [-b branch]|status|init|update|summary [-n|--summary-limit <n>] [<commit>]] [--] [<path>...]
class ActivityEntry < ActiveRecord::Base
belongs_to :target, :polymorphic => true
belongs_to :user
# Target Scopes
named_scope :for, lambda {|obj| {:conditions => {:target_id => obj.id, :target_type => obj.class.name}} }
named_scope :for_listings, {:conditions => {:target_type => 'Listing'} }
named_scope :manageable_by, lambda {|user|
case
when user.can_manage_company? then {}
@rubiety
rubiety / gist:29404
Created November 26, 2008 14:35 — forked from qrush/gist:29315
# If you ever needed an array of all ActiveRecord models in your Rails app (+ Session):
Dir["app/models/**/*.rb"].each {|r| require r}
subclasses_of(ActiveRecord::Base).map(&:class_name)
# Or *all* models:
ActiveRecord::Base.connection # Preloads driver constants
constants_before = Object.constants
Dir["app/models/**/*.rb"].each {|r| require r}
Object.constants - constants_before