Skip to content

Instantly share code, notes, and snippets.

@AWaselnuk
AWaselnuk / fixnum_to_bool.rb
Last active August 29, 2015 14:00
Boolean typecasting.
# from http://drawingablank.me/blog/ruby-boolean-typecasting.html
class Fixnum
def to_bool
return true if self == 1
return false if self == 0
raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
end
end
@AWaselnuk
AWaselnuk / round_float.coffee
Last active August 29, 2015 14:07
Rounding floats to a given precision.
# From: http://www.jacklmoore.com/notes/rounding-in-javascript/
round = (value, decimals) ->
Number(Math.round(value + 'e' + decimals) + 'e-' + decimals)
@AWaselnuk
AWaselnuk / github_template_setup
Created October 8, 2014 20:40
Getting started with a template from Github
git clone git@github.com:[templateUser]/[templateRepo].git [yourNewRepoName]
cd [yourNewRepoName]
rm -rf .git
git init
git remote add origin git@github.com:[yourUserName]/[yourNewRepoName].git
git add -A
git commit -m "initial commit"
@AWaselnuk
AWaselnuk / timestamp.js
Created October 10, 2014 16:43
Get a UTC timestamp
Math.floor((new Date()).getTime())
@AWaselnuk
AWaselnuk / team.rb
Last active August 29, 2015 14:07
Test ActiveRecord callbacks
class Team < ActiveRecord::Base
has_many :users
validates :name, presence: true, uniqueness: true
before_destroy :clear_team_members
private
# Clear team members when a team is destroyed
@AWaselnuk
AWaselnuk / rubocop.yml
Created October 31, 2014 13:55
Rubocop Rails config
AllCops:
Include:
- '**/Rakefile'
- '**/config.ru'
Exclude:
- 'db/**/*'
- 'bin/**/*'
- 'config/**/*'
- 'node_modules/**/*'
- '**/rails_helper.rb'
@AWaselnuk
AWaselnuk / improved_set_interval.js
Created November 11, 2014 15:35
Improved setInterval
// BAD
setInterval(function(){
doStuff();
}, 100);
// GOOD
(function loopMe(){
doStuff();
setTimeout(loopMe, 100);
})();
@AWaselnuk
AWaselnuk / load_script.js
Created November 19, 2014 16:32
Load external javascript files asynchronously
//this function will work cross-browser for loading scripts asynchronously
//from: http://stackoverflow.com/questions/7718935/load-scripts-asynchronously
function loadScript(src, callback)
{
var s,t,done;
done = false;
s = document.createElement('script');
s.type = 'text/javascript';
s.src = src;
s.onload = s.onreadystatechange = function() {
@AWaselnuk
AWaselnuk / this.js
Last active August 29, 2015 14:10
Understanding 'this' in Javascript.
// Summary from http://www.sitepoint.com/what-is-this-in-javascript/
//// GLOBAL SCOPE
// If there's no current object, 'this' refers to the global object
window.WhoAmI = "I'm the window object"
console.log(window.WhoAmI) // "I'm the window object"
@AWaselnuk
AWaselnuk / sentinal.js
Created November 25, 2014 16:31
Wait for dependencies to arrive before executing code
// From: http://stackoverflow.com/questions/16425520/angularjs-test-that-an-external-script-is-effectively-loaded
(function waiter(){
if(!window.jQuery){ return setTimeout(waiter, 37); }
$("#myDiv").fadeOut();
}())