Skip to content

Instantly share code, notes, and snippets.

View mihar's full-sized avatar
🤠
We the best

Miha mihar

🤠
We the best
  • Global
View GitHub Profile
@mihar
mihar / slurpah.js
Created May 22, 2016 00:50 — forked from jenbennings/slurpah.js
slurpah.js
(function(){
var externalStylesheets = [];
var fonts = {};
function findExternalStylesheets(obj, callback) {
sheet = document.styleSheets,
i = sheet.length,
rule = null;
while( 0 <= --i ) {
if (sheet[i].cssRules === null) {
@mihar
mihar / simple_state_machine.rb
Last active December 30, 2015 23:28
A simple ActiveRecord state machine for Mongoid
module SimpleStateMachine
# Usage:
#
# First, extend your model of choise with this module
#
# extend SimpleStateMachine
#
# Then you can call the method states() and pass all the states
# that your model supports. E.g.:
#
@mihar
mihar / orderbook.md
Last active December 10, 2015 23:15

Orderbook price calculation

Write a function btc2usd that calculates how much dollars you need to buy X bitcoin at this moment.

A sample asks part of the orderbook looks like this:

asks = [['1.00000000', '100.00', 1], ['2.00000000', '200.00', 2], ['4.00000000', '400.00', 4], ['8.00000000', '800.00', 8], ['16.00000000', '1600.00', 16]]
//        |              |       |    
// amount of btc price per BTC no. of equal orders
@mihar
mihar / keybase.md
Created February 3, 2015 01:16
keybase.md

Keybase proof

I hereby claim:

  • I am mihar on github.
  • I am mihar (https://keybase.io/mihar) on keybase.
  • I have a public key whose fingerprint is 8C74 E3A9 2C2D BF69 11F9 2DA1 BA08 69AE 85A1 6F9E

To claim this, I am signing this object:

@mihar
mihar / gist:10988181
Last active August 29, 2015 13:59
Figure out if all booleans are true
array.reduce(true) { |x, y| x and y }

Useful for checking in Rails if a collection of models all have a flag set to true.

E.g.:

signers.map(&:ready?).reduce(true) { |x, y| x and y }

More examples:

@mihar
mihar / index.html
Created January 10, 2014 11:04 — forked from mdakram/index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>App Redirection</title>
</head>
<body>
<!-- iframe used for attempting to load a custom protocol -->
<iframe style="display:none" height="0" width="0" id="loader"></iframe>
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<ul>
DUBJOY.human_time = (s) ->
time = DUBJOY.get_time_from_seconds s
output = ""
output += "#{time.h}h" if time.h and time.h isnt '00'
output += "#{time.m}m" if time.m and time.m isnt '00'
output += "#{time.s}s" if time.s and time.s isnt '00'
output += "#{time.ms}ms" if time.ms and time.ms isnt '00'
output
@mihar
mihar / gist:1263820
Created October 5, 2011 06:55
JavaScript equivalent of Array#to_sentence from Rails
Array.prototype.to_sentence = function() {
return this.join(", ").replace(/,\s([^,]+)$/, ' and $1')
}
@mihar
mihar / gist:1263600
Created October 5, 2011 04:02
Remove element from array in JavaScript
Array.prototype.remove = function(element) {
this.splice(this.indexOf(element), 1)
}