Skip to content

Instantly share code, notes, and snippets.

class Module
# is there a given distinct method defined on the instance of a class?
def distinct_method_defined? method_name
distinct_instance_methods.include? method_name.to_sym
end
# display the methods - including inherited - that distinguish this class's instances
def distinct_instance_methods
exclude = self == Object ? Kernel.public_instance_methods : Object.public_instance_methods
public_instance_methods - exclude
@acook
acook / strings.rb
Last active August 29, 2015 13:55
All you never wanted to know about Strings in Ruby.
# the simple stuff
string = 'uninterpolated string'
string = "interpolated\nstring"
# if you have all kinds of quotes in your strings, these are available if you must
string = %q{uninterpolated string}
string = %Q{interpolated\nstring}
#heredocs!
string = <<END
Bundler could not find compatible versions for gem "axiom":
In snapshot (Gemfile.lock):
axiom (0.2.0)
In Gemfile:
rom (>= 0.1.2) ruby depends on
axiom (~> 0.1.1) ruby
# Create UNION qeuries between ActiveRecord 3.x Relation objects.
# inspired by: https://coderwall.com/p/9hohaa
module Union
def union *relations
opts = relations.extract_options!
model = ((is_a?(ActiveRecord::Base) || is_a?(ActiveRecord::Relation)) && self) || relations.first.klass
query = 'SELECT'
query << ' DISTINCT' if opts[:distinct]
module Assertions
module_function
def assert_close_enough value1, value2
assert_equal value1.round, value2.round
end
end
@acook
acook / gist:9006975
Last active August 29, 2015 13:56 — forked from anonymous/gist:9006967
class TracJob
def initialize action, repo, rev
@action, @repo, @rev = action, repo, rev
end
attr :action, :repo, :rev, :failures
def run trac_projs
trac_projs.each do |project|
success = trac_admin_success? project
failures << tp unless success
@acook
acook / .gitignore
Last active August 29, 2015 13:56 — forked from gabetax/benchmark.rb
factorygirl*
new*
Gemfile.lock
server: open tcp://localhost:4321
until [error? try [insert server ask "R> "]]
close server
relation = Model .where nil
table = relation.arel_table
first = table[:id] .eq 3
third = relation.where id: 1
where_third = third.arel.ast.cores.last.wheres
relation.where first . or where_third
module AnyOf # makes "OR" queries
def any_of *queries
queries.inject(arelify queries.pop) do |these, query|
these.or arelify query
end
end
private
def arelify query # tempting to move this into a "to_arel" method