Skip to content

Instantly share code, notes, and snippets.

@acook
acook / .gitignore
Last active August 29, 2015 13:56 — forked from gabetax/benchmark.rb
factorygirl*
new*
Gemfile.lock
@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
module Assertions
module_function
def assert_close_enough value1, value2
assert_equal value1.round, value2.round
end
end
# 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]
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
@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
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
firstyear
=> "1885-1886"
lastyear
=> "1884-1885"
firstyear > lastyear
=> false
firstyear.bytes
=> [49, 56, 56, 53, 45, 49, 56, 56, 54]
@acook
acook / setters_are_special.rb
Created January 20, 2014 17:52
Even with an explicit return, setters always return the original value. This makes some sense, but it can be a gotcha if you're doing something special like accepting a string an setting a File object, instead of getting the file back, you'll still get the string.
class Foo
def bar= new_bar
return "MEGAZOIDS"
end
end
f = Foo.new
p f.bar = "lolcats" #=> "lolcats"
@acook
acook / local_init_stupidness.rb
Created January 20, 2014 17:46
WHY RUBY?! WHYYYYY
def foo
if false then
x = 1
end
x
end
foo #=> nil, where you'd think it would error