Skip to content

Instantly share code, notes, and snippets.

@cantin
cantin / ast_call_finder.js
Created August 4, 2022 02:16
I18n-tasks JS AST parser call finder function
let fs = require('fs')
let parser = require("@babel/parser")
let traverse = require("@babel/traverse")
function collectCalls(filepath) {
let results = []
let code = fs.readFileSync(filepath).toString()
let ast = parser.parse(code, {
// parse in strict mode and allow module declarations
@cantin
cantin / pair.rb
Last active September 3, 2019 13:51
GuangZhou RailsGirs Pair
tutors = ["***", "***", "***"]
girls = ["***", "***", "***"]
number = "To be defined"
tutors = tutors.shuffle(random: Random.new(number))
girls = girls.shuffle(random: Random.new(number))
result = Hash.new { |hash, key| hash[key] = [] }
tutors.cycle(3).each_with_index do |tutor, i|
result[tutor] << (girls[i] || "未配对")
//Accept zoneStr such as '-0400', '-04:00', '0400', '+0400'
//Return such as 4, -4, -4.75, -4.5
function tranformTimeZone(zoneStr) {
var signSymbol, hours, minutes, zone;
if (/^\+|-/.test(zoneStr)) {
signSymbol = zoneStr[0];
zoneStr = zoneStr.slice(1);
} else {
signSymbol = '+';
class Iterator < Enumerator
def initialize obj, meth, *args
super() do |y|
loop do
y << obj
obj = obj.send(meth, *args)
end
end
end
end
@cantin
cantin / downgrade_rubygem.md
Created December 10, 2012 06:49
downgrade rubygem

Due to the rails2.x is not compatible wtih RubyGem > 1.6, need downgrade RubyGem if run rails2.x app

gem uninstall rubygems-update
gem install rubygems-update -v 1.4.2
update_rubygems
@cantin
cantin / how_it_works.md
Created December 3, 2012 03:12 — forked from rubiii/how_it_works.md
MacVim-Formatter for RSpec
$ rspec --format MacVimFormatter --color spec
@cantin
cantin / dump_sql.rb
Created November 20, 2012 08:10
dump sql and reload
> DB_STRUCTURE=db/null_schema.sql rake db:structure:dump
class NullMigration < ActiveRecord::Migration
def up
file_data = File.read('db/migrate/null_schema.sql')
ActiveRecord::Base.connection.execute file_data
end
def down
@cantin
cantin / forwardable.rb
Created November 9, 2012 09:45
ruby-stb-lib forwardable
require 'forwardable'
class A
extend Forwardable
def initialize
@arr = Array.new
end
def_delegator :@arr, :push, :add
@cantin
cantin / default params for block.rb
Created November 8, 2012 09:55
lambda default params
f = lambda {|x, y=1| puts(x+y) }
f = ->(x, y =1) { puts(x+y) }
@cantin
cantin / mysql
Created November 8, 2012 04:10
msyql dump
--export
mysql -u user -p password database_name > export.sql
mysql -u user -p password database_name table_name > export.csv
--import
mysql -u user -p password
source export.sql