Skip to content

Instantly share code, notes, and snippets.

View workmad3's full-sized avatar

David Workman workmad3

View GitHub Profile
def test(callable)
callable.call
puts "here"
end
test(lambda{return}) #=> "here"
test(proc{return}) #=> LocalJumpError
class Order < ActiveRecord::Base
named_scope :order_counts, lambda { |*args| {:joins => :customer,:select => 'COUNT(DISTINCT customer_id) customer_id,category_id,product_price,quantity, SUM(product_price * quantity) AS my_sum', :group => 'category_id', :conditions => ['orders.created_at <= ? AND customers.temp = ?',args[0], args[1]]}}
end
Order.order_counts.first.my_sum
<h3><%= link_to (!(administrator.first_name.blank? && administrator.last_name.blank?) ? administrator.first_name + ' ' + administrator.last_name : administrator.email), admin_user_path(administrator) %></h3>
class Objective < ActiveRecord::Base
before_save :check_state
ImmutableAttributesByState = { :open => [], :agreed => [:attrib1, :attrib2, :attrib3] }
def check_state
!ImmutableAttribuesByState[state].any? {|attribute| self.send(:"#{attribute}_changed?") }
end
end
class String
def reverse_with_print
rev = reverse_without_print
print rev
return rev
end
alias :reverse_without_print :reverse
alias :reverse_with_print :reverse
end
module Enumerable
def inject_with_index(primer = nil)
each_with_index.inject(primer) do |acc, arr_with_idx|
yield acc, arr_with_idx[0], arr_with_idx[1]
end
end
end
[:a, :b, :c].inject_with_index({}) {|hsh, sym, idx| hsh.tap {|h| h[sym] = idx}}
class List
attr_accessor :per_page, :distribution, :page_count, :item_count
def initialize(per_page=nil)
@per_page = per_page || 21
@page_count = -1
@item_count = -1
@distribution = []
end
end
class MyClass
def self.draw_some_routes(mapper)
mapper.resources :accounts, :controller => 'authr/accounts', :only => [:new, :create]
end
end
Rails.application.routes.draw do |map|
MyClass.draw_some_routes(self)
end
class Admin < Person
end
Autotest.add_discovery { "rails" }
Autotest.add_discovery { "rspec2" }