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 / 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 / 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:

### Inside the helper file that uses it.
class Spitter
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::UrlHelper
def initialize(klass)
@klass = klass
end
@mihar
mihar / magic.rb
Created December 14, 2009 15:55 — forked from mislav/magic.rb
require 'active_record'
class ActiveRecord::Base
def self.magic!
connection.tables.map { |table|
klass = Class.new(self)
Object.send(:const_set, table.singularize.camelize, klass)
}.each { |model|
model.column_names.grep(/_id$/).each { |foreign_key|
name = foreign_key.sub(/_id$/, '')
require 'rubygems'
require 'activesupport'
# Lorem Ipsum
words = %w{lorem ipsum dolor sit amet consectetuer adipiscing elit phasellus hendrerit pellentesque aliquet nibh nec urna in nisi neque vel dapibus id mattis sed pretium ligula sollicitudin laoreet viverra tortor libero sodales leo eget blandit nunc eu nullam mollis ut justo suspendisse potenti egestas ante et vulputate volutpat eros pede semper est vitae luctus metus augue morbi purus faucibus commodo quis gravida lectus praesent elementum at felis vestibulum lacus a ultrices sagittis mi euismod dui pulvinar sapien ornare nisl arcu fermentum interdum ac risus lacinia magna ullamcorper facilisis mauris varius tellus etiam ultricies integer fusce accumsan diam vehicula sem massa malesuada non congue scelerisque vivamus tristique iaculis imperdiet feugiat convallis odio aliquam posuere nulla quisque cum sociis natoque penatibus magnis dis parturient montes nascetur ridiculus mus condimentum dignissim proin donec curabitur}
@words = words.reject { |word|
@mihar
mihar / gist:1230791
Created September 21, 2011 00:06
Sinatra CSS/SASS wildcard route
get '/*.css' do
content_type 'text/css', :charset => 'utf-8'
sass params[:splat].join.to_sym
end
@mihar
mihar / gist:1239740
Created September 24, 2011 19:29
Stripping HTML tags from a string in JavaScript
String.prototype.strip_html = function() {
var tmp = document.createElement("DIV");
tmp.innerHTML = this;
return tmp.textContent || tmp.innerText;
}
@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)
}
@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 / 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.:
#