Skip to content

Instantly share code, notes, and snippets.

View avdi's full-sized avatar

Avdi Grimm avdi

View GitHub Profile
require 'equalizer'
class EqualizableStruct < Struct
def initialize *args, &block
class << self
include Equalizer.new(*members)
end
super
end
@avdi
avdi / Ruby.markdown
Created February 18, 2013 20:09 — forked from jcoglan/Ruby.markdown

Core ideas

  • An Object is a set of instance variables and a pointer to a 'singleton class'.
  • Properties are looked up in the instance variables, methods are dispatched via the singleton class.
  • Module is a subtype of Object. A Module is a set of methods and an ordered list of zero-or-more 'parent' modules.
  • Module A becomes a parent of module B via B.include(A).
  • Method lookup works by doing a depth-first right-to-left search of a module tree.
  • Class is a subtype of Module. A Class is a Module that can be instantiated.
  • A Class has only one 'superclass'. A class includes its superclass as its first parent module for the purposes of method dispatch. A class's singleton class includes the superclass's singleton class as its first parent.
  • The default superclass of all classes is Object.
# If you don't want to force the composed methods to raise exceptions,
# have them take a block specifying the failure action
def composed_method
frobulate_widgets { return false }
refrobulate_widgets { return false }
confribulate_frobulations { return false }
true
end
@avdi
avdi / task.cs
Created December 17, 2012 23:35 — forked from kberridge/task.cs
public class Task : ActiveRecord
{
public string Name { get; set; }
public int AssignedTo_UserId { get; set; }
public DateTime DueOn { get; set; }
public Task()
{
DueOn = DateTime.Now.AddDays(1);
}
@avdi
avdi / hack.rb
Created May 17, 2012 17:34 — forked from lmarburger/hack.rb
Possible for a module included somewhere to override a class's instance method?
class Base
def call
'call'
end
end
Base.new.call # => "call"
module Override
def new(*)
@avdi
avdi / virtus-coercion.rb
Created May 11, 2012 22:07 — forked from solnic/virtus-coercion.rb
ValueObject in Virtus with a custom constructor - proposed
require 'virtus'
Point = Struct.new(:x, :y)
class Rectangle
include Virtus
attribute :top_left, Point
attribute :bottom_right, Point
@avdi
avdi / gist:1516514
Created December 24, 2011 06:10 — forked from saturnflyer/gist:1515021
allowing objects to specify exception types
class Share
def call
list.each do |address|
referral = new_referral(address)
# All else being equal, I'd probably prefer a caller-specified
# fallback strategy
referral.save do
@unsaved << referral
end
require 'spec_helper'
describe Feeds::Drops::Rss::EntryDrop do
use_vcr_cassette
subject {
Feeds::Drops::Rss::EntryDrop.new(rss_app.feed_entries_to_display[1], context)
}
let(:context) { mock('context') }
let(:rss_app) {