Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/ruby
# -*- coding: utf-8 -*-
$KCODE = 'u'
require 'csv'
File.open('tmp.csv', 'wb') do |csv_file|
# 输出标明文本编码为 UTF-8 的 BOM
csv_file.write("\xEF\xBB\xBF")
@anvyzhang
anvyzhang / pretty-print.scala
Created September 8, 2016 03:57 — forked from carymrobbins/pretty-print.scala
Pretty print Scala case classes and other data structures.
/**
* Pretty prints a Scala value similar to its source represention.
* Particularly useful for case classes.
* @param a - The value to pretty print.
* @param indentSize - Number of spaces for each indent.
* @param maxElementWidth - Largest element size before wrapping.
* @param depth - Initial depth to pretty print indents.
* @return
*/
private def prettyPrint(a: Any, indentSize: Int = 2, maxElementWidth: Int = 30, depth: Int = 0): String = {
@anvyzhang
anvyzhang / em-http-request.rb
Created October 24, 2013 08:32
Test async http request vs sync http request
require 'rubygems'
require 'eventmachine'
require 'em-http-request'
require 'patron'
f = Time.now
arr = []
j = 50
EventMachine.run do
(1..50).each{|i|
@anvyzhang
anvyzhang / mongoid_cursor.rb
Last active December 18, 2015 06:09
When a mongo collection is too large for RAM, use cursor this way.
App.db.collection('apps').find({'name' => 'test_name'},{:timeout => false}){|ptr| ptr.each{|x| puts x['user_id']}}
# NOTE in cursor iteration
ptr.each{|x|
x.user_id # WRONG
x['user_id'] # RIGHT
}