Skip to content

Instantly share code, notes, and snippets.

View tonytonyjan's full-sized avatar
🪲
Making bugs

簡煒航 (Weihang Jian) tonytonyjan

🪲
Making bugs
View GitHub Profile
@tonytonyjan
tonytonyjan / autoloader.rb
Created April 21, 2018 02:55
Autoloader loads ruby files lazily
module AutoLoader
def self.included(mod)
caller_path, = caller(1..1).first.partition(':')
pattern = "#{File.dirname(caller_path)}/#{File.basename(caller_path, '.rb')}/*.rb"
Dir.glob(pattern).each do |path|
class_name = ::Utils.classify(File.basename(path, '.rb')).to_sym
mod.autoload class_name, path
end
end
end
@tonytonyjan
tonytonyjan / benchmark.rb
Last active September 20, 2017 14:03
`[4, 5].max` is slower than `[a, b].max` with ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
require 'benchmark'
n = 100000000
Benchmark.bmbm do |x|
x.report('[4, 5].max') { n.times { [4, 5].max } }
a, b = 4, 5
x.report('[a, b].max') { n.times { [a, b].max } }
end
@tonytonyjan
tonytonyjan / dasherize.sh
Created April 23, 2017 02:26
Convert any string to to lowercase and dashes fromat
#!/bin/sh
# $ dasherize Hello, world! I am tonytonyjan.
printf "$*" \
| tr '[:upper:]' '[:lower:]' \
| tr -C '[:alnum:]' ' ' \
| tr -s ' ' '-' \
| sed -e 's/^-*//' -e 's/-*$//' \
| tr -d '\n'
@tonytonyjan
tonytonyjan / benchmark.rb
Last active June 4, 2017 06:41
How fast is `reverse!` technique.
# conversation: https://github.com/rack/rack/commit/734a00c5f4bb46e9a5e6e2677d89a2f285dcc185
#
# user system total real
# reverse: 0.010000 0.010000 0.020000 ( 0.024657)
# join: 0.050000 0.010000 0.060000 ( 0.056571)
# regexp: 0.050000 0.000000 0.050000 ( 0.048339)
require 'benchmark'
def join(session_data)
@tonytonyjan
tonytonyjan / README.md
Last active December 3, 2016 08:10
rack-protection minimal example

Usage

gem install 'rack-protection'
ruby server.rb

There's a page with 2 forms, the one without CSRF token field will get 403 Forbidden response.

Note

@tonytonyjan
tonytonyjan / interactor.rb
Last active July 20, 2016 15:25
Minimal service object implementation
class Interactor
attr_reader :error
def self.perform(*args)
new(*args).tap { |interactor| catch(:fail) { interactor.perform } }
end
def success?
@error.nil?
end
@tonytonyjan
tonytonyjan / 1-README.md
Last active June 21, 2023 06:14
Remote React Components Loading
@tonytonyjan
tonytonyjan / chat.rb
Created March 23, 2016 14:02
Simple thread based chat server
require 'socket'
class Client
def initialize server, socket
@server, @socket = server, socket
start
end
def send message
@socket.puts message
@tonytonyjan
tonytonyjan / rails_42_with_active_support.rb
Last active May 18, 2022 10:31
session cookie decrypter for Rails
require 'cgi'
require 'json'
require 'active_support'
def verify_and_decrypt_session_cookie(cookie, secret_key_base)
cookie = CGI::unescape(cookie)
salt = 'encrypted cookie'
signed_salt = 'signed encrypted cookie'
key_generator = ActiveSupport::KeyGenerator.new(secret_key_base, iterations: 1000)
secret = key_generator.generate_key(salt)
@tonytonyjan
tonytonyjan / 0-main.rb
Created March 11, 2016 09:23
Use regular expression to solve integer factorisation problem
n = ARGV.first.to_i
fact = Hash.new(0)
while match_data = /^(.{2,}?)\1+$/.match('.'*n)
fact[match_data[1].length] += 1
n /= match_data[1].length
end
fact[n] += 1
puts fact.sort.map!{|k,v| "#{k}#{"**#{v}" if v > 1}"}.join(' * ')