Skip to content

Instantly share code, notes, and snippets.

View IanWhitney's full-sized avatar

Ian Whitney IanWhitney

View GitHub Profile
# Referenced in Design Is Refactoring post http://designisrefactoring.com/2016/02/03/factories/
# This code takes the previous example and extends it. Classes can now define 'roles' they satisfy
# and each class can satisfy more than one role.
#
# At load time, these roles are examined and recorded in registries.
# In this example there's a CampusLikeRegistry that contains every class that says it's campus-like.
# But you could have any number of registries.
require 'singleton'
require 'forwardable'
[1,2,3][3]
#=> nil
# Ask for element outside of the array, get nil. cool.
[1,2,3][3,1]
#=> []
#=> Ask for a 1-element slice that starts outside of the array, get an empty array. Ok?
[1,2,3][4]
#=> nil
@IanWhitney
IanWhitney / script.rb
Created September 15, 2015 14:07
delete a Receipt's facets
receipt_id = _your_receipt_id_
r = Etl::Receipt.find(receipt_id)
r.audits.each do |a|
Etl::Audit::Facet.all_classes.each do |f|
f.where(etl_audit_id: a.id).delete_all
end
end
s = Etl::Service.new(r)
s.extract_facets
# From the docs of Kernel.array
# First tries to call to_ary on arg, then to_a.
# http://ruby-doc.org/core-2.2.3/Kernel.html#method-i-Array
# Ok, so does a struct respond to to_ary?
Monkey = Struct.new(:name, :favorite_color)
m = Monkey.new("George", "Yellow")
m.respond_to?(:to_ary)
#=> false
class Bob
def reply_to(statement)
statement.reply(self)
rescue
default_reply
end
def reply_to_silence
"Fine. Be that way!"
end
class Bob
def reply_to(statement)
public_send("reply_to_#{statement.class}".downcase.to_sym)
rescue NoMethodError
default_reply
end
def reply_to_silence
"Fine. Be that way!"
end
GIT
remote: git://github.com/rails/rails.git
revision: 5d543c5996da986ba8d979cd83236e130a3bd953
tag: v4.2.0.rc1
specs:
actionmailer (4.2.0.rc1)
actionpack (= 4.2.0.rc1)
actionview (= 4.2.0.rc1)
activejob (= 4.2.0.rc1)
mail (~> 2.5, >= 2.5.4)
@IanWhitney
IanWhitney / examples.rb
Last active August 29, 2015 14:10
Struct and OpenStruct: Array conversions
# Array of an openstruct works as you'd expect, but to_a does not.
# OpenStruct descends from Object, Kernel, BasicObject. None of which implement to_a. So that explains that.
# I'm a little unclear on why Array(os) is working. Array(Object.new) works, and OpenStruct descends directly from Object.
# BUT, the Array docs say that it calls .to_ary or .to_a on the argument, and an OpenStruct instance responds to neither of those.
# So....?
require 'ostruct'
#=> true
os = OpenStruct.new
#=> #<OpenStruct>
os.prop1 = "test"
@athlete = Athlete.find(4329615)
@active_progs = @athlete.future_complete_academic_program_memberships.all(:conditions => "prog_status in ('AC', 'CM')")
@old_progs = @athlete.future_complete_academic_program_memberships.all(:conditions => "prog_status not in ('AC', 'CM')", :order => "action_dt desc")
@academic_tree_paths = @athlete.future_academic_tree_paths
prog = @active_progs.first
@academic_tree_paths.detect { |tp| tp.ps_acad_prog_stdnt_car_nbr == prog.stdnt_car_nbr && tp.ps_acad_prog_acad_career == prog.acad_career}
@IanWhitney
IanWhitney / hash.rb
Created September 15, 2014 14:34
Hash creation with default empty array
x = Hash.new{|h,k| h[k] = []}