Skip to content

Instantly share code, notes, and snippets.

@geeksam
geeksam / config-initializers-breadcrumb.rb
Created October 13, 2015 23:55
Handy pattern for puts-statement debugging in Rails
if Rails.env.test? || Rails.env.development?
module Kernel
def __breadcrumb__(msg = "")
return unless $debug
msg = msg.to_s
breadcrumb = ">>> #{caller.first}"
breadcrumb << "\n--> #{msg}" if msg.present?
Rails.logger.debug breadcrumb
puts '', breadcrumb
end
@geeksam
geeksam / counts_by.rb
Last active March 21, 2017 19:32
Enumerable#counts_by
module Enumerable
def counts_by(&property)
group_by(&property) # Group my values by some interesting property
.lazy # (in a potentially more GC-friendly way),
.map {|x,xs| [ x, xs.length ] } # collapsing the sub-lists of values to simple counts...
.sort_by(&:last) # and show them in increasing order of frequency (so that,
# if the list is very long, the high numbers are at the end)
end
end