Skip to content

Instantly share code, notes, and snippets.

# We create an index to store all of the comments in the tree,
# indexed by ID. This lets us quickly build out the comment structure
# as we iterate over each comment.
node_index = ActiveSupport::OrderedHash.new
nodes.each do |node|
node_index[node.id] = {
node: node,
parent: nil,
reply_to: nil,
children: []
# We use includes(:member) to solve one of the N+1 issues by loading
# all of the members in a single query.
serialized_comments = article.
comments.
includes(:member).
arrange_serializable do |comment, children|
# This block is invoked for every node in the tree, which lets us
# build out a tree of serialized comments.
Api::V1::CommentSerializer.new(comment, children: children)
end
# Is there a better way to achieve this without having to define #orig_foo in each subclass?
class A
def orig_foo
self.class.superclass == Object ? foo : method(:foo).super_method.call
end
def foo
"foo"
end
end
@meltingice
meltingice / benchmark.md
Created January 20, 2016 23:50
PSD.rb vs psd_native

PSD.rb

Small PSD

➜  psd.rb git:(master) ✗ bundle exec ruby examples/export_layer_images.rb benchmark_small.psd
      user     system      total        real
  1.370000   0.490000   1.860000 (  1.861384)

Keybase proof

I hereby claim:

  • I am meltingice on github.
  • I am ryanlefevre (https://keybase.io/ryanlefevre) on keybase.
  • I have a public key whose fingerprint is BEA0 C90A 92D1 C161 2ED8 4DF4 BDEC 0849 9B28 19A8

To claim this, I am signing this object:

10/15/14 2:41:17.987 PM Sonos[80437]: (SCLib) msrch(1): Expect self m-search
10/15/14 2:41:17.987 PM Sonos[80437]: (SCLib) msrch(0): Failed to setup MSearchNotifyHandler.
9/16/14 9:48:24.752 AM Sonos[20995]: (SCLib) msrch(0): Failed to setup MSearchNotifyHandler.
9/16/14 9:48:24.753 AM Sonos[20995]: (SCLib) msrch(1): Expect self m-search
class Object
def self.inherited(subclass)
subclass.send(:prepend, Module.new do
def inspect
"naw bawg"
end
end)
super
end
end
@meltingice
meltingice / gist:ea3547c0023321786b7a
Created May 15, 2014 13:40
Ruby: Initializing class instance variables with default values for all subclasses
class ParentClass
@foo = "bar"
class << self
attr_accessor :foo
def inherited(subclass)
subclass.instance_variable_set :@foo, "bar"
super
end
module Async
def async(method)
prepend(Module.new {
define_method(method) do |*args|
Thread.new(*args) do |*args|
puts Thread.current.object_id.to_s(16)
super(*args)
end
end
})