Skip to content

Instantly share code, notes, and snippets.

View ifyouseewendy's full-sized avatar

Di Wen ifyouseewendy

View GitHub Profile
# SPOJ - PRIME1 http://www.spoj.com/problems/PRIME1/
#
# *NOTE*
#
# 1. Presieve the primes under sqrt(1_000_000_000).
# 2. Multiple the primes to flag numbers which are not primes.
# 3. Sieve in given range.
primes = [2]
@ifyouseewendy
ifyouseewendy / ruby_trick.md
Created March 1, 2015 08:28
some snippets

How to check N is a power of 2 ?

def is_a_power_of_2?(n)
  puts "#{n} is a power of 2!" if n.bit_length != (n-1).bit_length
end

from Binary and Bitwise Operations in Ruby by CooperPress http://goo.gl/4uQY7f

Bitwise

Representation

How to represent base 2, 8, 10, 16 in Integer?

HOST = ""
APP_KEY = ""
APP_SECRET = ""
REQUEST_TOKEN = ""
REQUEST_TOKEN_SECRET = ""
REQUEST_TOKEN_VERIFIER = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
@ifyouseewendy
ifyouseewendy / update_git_commit.sh
Created March 25, 2015 09:20
Update git commit history using git-filter-branch
git filter-branch -f --commit-filter '
if [ "$GIT_COMMITTER_NAME" = "<Old name>" ];
then
GIT_COMMITTER_NAME="<New name>";
GIT_COMMITTER_EMAIL="<New email>";
GIT_AUTHOR_NAME="<New name>";
GIT_AUTHOR_EMAIL="<New email>";
else
git commit-tree "$@";
fi' -- --all
@ifyouseewendy
ifyouseewendy / local_assignment_over_method_sending.md
Created March 30, 2015 08:29
Remember, local assignment has precedence over method sending.

There is a weird situation I haven't noticed before:

class Counter
  attr_accessor :processed, :processed_names

  def initialize
    @processed = 0
    @processed_names = []
 end
# API
#
# + push
# + top
# + pop
# + empty
# + empty?
ArrayStack = Struct.new(:values) do
def push(value)
@ifyouseewendy
ifyouseewendy / js_pass_protect_from_forgery.md
Last active August 29, 2015 14:23
Make pure js POST request pass the `protect_from_forgery`, without Rails helper.

csrf.js

var csrftk = $("meta[name='csrf-token']").attr("content");

// Ajax set
$(document).ajaxSend(function(e, xhr, options) {
  xhr.setRequestHeader("X-CSRF-Token", csrftk);
});