Skip to content

Instantly share code, notes, and snippets.

View jvsidler's full-sized avatar

Jim Sidler jvsidler

View GitHub Profile
@jvsidler
jvsidler / active_validator.rb
Created August 10, 2011 16:18
Ruby on Rails: Custom Validator
class ActiveValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
# If the relationship exists and the related object is active
unless eval("record.#{attribute.to_s.gsub("_id", "")}") && eval("record.#{attribute.to_s.gsub("_id", "")}").active
record.errors.add(attribute, "must be an active #{attribute.to_s.gsub("_id", "")}") unless eval("record.#{attribute}.blank?")
end
end
end
@jvsidler
jvsidler / ranges.rb
Created August 19, 2011 20:38
Technical interview answer
if ARGV.empty?
puts "\nPlease enter any two SEQUENTIAL random numbers separated by one space. i.e 16 21\n"
a, b = gets.strip.split(" ")
else
a, b = ARGV[0], ARGV[1]
end
if a.to_i > 0 && b.to_i == 0
puts "Please give two numbers."
exit
@jvsidler
jvsidler / gist:4610255
Created January 23, 2013 17:14
List all crontabs for all users on UNIX systems. Must be root. Credit: http://stackoverflow.com/questions/134906/how-do-i-list-all-cron-jobs-for-all-users
for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done
@jvsidler
jvsidler / gist:7548312
Created November 19, 2013 16:38
Handy one liner to recover contents of unintentionally cleared stashes. Recover work after 'git stash clear'.
git fsck --unreachable | grep commit | cut -d\ -f3 | xargs git show
git stash apply <commit sha-1>