This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>...] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |