Skip to content

Instantly share code, notes, and snippets.

Avatar

David Michael dmichael

View GitHub Profile
View gist:118757
# 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)}
View gist:118773
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
View gist:118776
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
View gist:119362
class Jovial
def self.laugh
puts "ha."
end
end
Affable = Jovial
Affable.laugh
# => ha.
View gist:122313
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`
View gist:122533
class Soundfile
if windows?
def info
...
end
else
def info
...
end
end
View gist:122528
class Soundfile
# Windows
def info
`./bin/sndfile-info.exe`
end
# *nix
def info
`./bin/sndfile-info`
View gist:122535
class Soundfile
attr_accessor :info_bin
def initialize
@info_bin = (windows?) ? 'sndfile-info.exe' : 'sndfile-info'
end
end
View Inversion of control.rb
class SoundFile
attr_reader :binaries
delegate :info, :to => :binaries
def initialize
@binaries = Binaries.instance
end
end
module Binaries