Skip to content

Instantly share code, notes, and snippets.

View dmichael's full-sized avatar

David Michael dmichael

View GitHub Profile
# Order one Array by the order of another Array in Ruby.
expected = [:john, :moishe, :bartholemew]
received = [:moishe, :bartholemew, :john]
sorted = received.sort_by {|name| expected.index(name)}
# => [:john, :moishe, :bartholemew]
# It should even handle shorter lists.
sorted = [:bartholemew, :john].sort_by{|name| expected.index(name)}
module SetNotationHelper
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
# Here's our little magic hook
def inherited(subclass)
subclass.class_eval do
class YourKillerModel
# This method checks permissions for the :index action
def self.is_indexable_by(user, parent = nil)
end
# This method checks permissions for the :create and :new action
def self.is_creatable_by(user, parent = nil)
end
# This method checks permissions for the :show action
def is_readable_by(user, parent = nil)
end
class Jovial
def self.laugh
puts "ha."
end
end
Affable = Jovial
Affable.laugh
# => ha.
class Sndfile
begin
helper = File.join(root, 'win32', 'sndfile-info.exe')
`#{helper} --version`
rescue Exception => e
puts e.inspect
begin
helper = File.join(root, 'osx', 'sndfile-info')
`#{helper} --version`
class Soundfile
# Windows
def info
`./bin/sndfile-info.exe`
end
# *nix
def info
`./bin/sndfile-info`
class Soundfile
if windows?
def info
...
end
else
def info
...
end
end
class Soundfile
attr_accessor :info_bin
def initialize
@info_bin = (windows?) ? 'sndfile-info.exe' : 'sndfile-info'
end
end
class SoundFile
attr_reader :binaries
delegate :info, :to => :binaries
def initialize
@binaries = Binaries.instance
end
end
module Binaries
module DickCheney
def self.speak
puts "meeeh..."
end
end
DickCheney.speak
# => meeeh...