Skip to content

Instantly share code, notes, and snippets.

View IanWhitney's full-sized avatar

Ian Whitney IanWhitney

View GitHub Profile
@IanWhitney
IanWhitney / links.md
Last active August 29, 2015 13:57
Reading material suggested during the Torquebox OOD class in Durham, 2014
@IanWhitney
IanWhitney / house.rb
Created March 14, 2014 00:45
Probably not what you want, right?
#!/usr/bin/env ruby
puts "This is the house that Jack built."
puts ""
puts "This is the malt that lay in the house that Jack built."
puts ""
puts "This is the rat that ate the malt that lay in the house that Jack built."
puts ""
puts "This is the cat that killed the rat that ate the malt that lay in the house that Jack built."
puts ""
@IanWhitney
IanWhitney / hash.rb
Created September 15, 2014 14:34
Hash creation with default empty array
x = Hash.new{|h,k| h[k] = []}
@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 / 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"
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)
class Bob
def reply_to(statement)
statement.reply(self)
rescue
default_reply
end
def reply_to_silence
"Fine. Be that way!"
end
# 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
@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
[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