ruby map简写

threads.map(&:join)
#等同于
threads.map{|thread|thread.join}

zerofill补零

"4".rjust(2, '0') #04
"%04d" % 5 #0005

~~~ruby json = {:key => ‘value’} JSON.parse(json) #{‘key’ => ‘value’} JSON.parse(json,{:symbolize_names => true}) #{:key => ‘value’} ``

Some globals have actual readable names:

$DEBUG     # The boolean status of the -d switch.
$FILENAME  # Current input file from ARGF. Same as ARGF.filename.
$LOAD_PATH # Load path for scripts and binary modules by load or require.
$stderr    # The current standard error output.
$stdin     # The current standard input.
$stdout    # The current standard output.
$VERBOSE   # The verbose flag, which is set by the -v switch.
But most dont:

$!  # The exception object passed to #raise.
$@  # The stack backtrace generated by the last exception raised.
$&  # Depends on $~. The string matched by the last successful match.
$`  # Depends on $~. The string to the left of the last successful match.
$'  # Depends on $~. The string to the right of the last successful match.
$+  # Depends on $~. The highest group matched by the last successful match.
$1  # Depends on $~. The Nth group of the last successful match. May be > 1.
$~  # The MatchData instance of the last match. Thread and scope local. MAGIC
$=  # The flag for case insensitive. Defaults to nil. Deprecated.
$/  # The input record separator (eg #gets). Defaults to newline.
$\  # The output record separator (eg #print and IO#write). Default is nil.
$,  # The output field separator for the print and Array#join. Defaults to nil.
$;  # The default separator for String#split. See -F flag.
$.  # The current line number of the last file from input.
$<  # See ARGF.
$>  # The default output for print, printf. Defaults to $stdout.
$_  # The last input line of string by gets or readline. Thread and scope local.
$0  # Contains the name of the script being executed. May be assignable.
$*  # See ARGV.
$$  # The process number of the Ruby running this script. Read only.
$?  # The status of the last executed child process. Read only. Thread local.
$:  # See $LOAD_PATH.
$"  # The array contains the module names loaded by require.

Many command line arguments have an associated global, which is usually just an alias to a real global:

$-0  # See $/.
$-a  # Autosplit mode. True if option -a is set. Read-only variable.
$-d  # See $DEBUG.
$-F  # See $;.
$-i  # In in-place-edit mode, this variable holds the extension, otherwise nil.
$-I  # See $LOAD_PATH.
$-l  # True if option -l is set. Read-only.
$-p  # True if option -p is set. Read-only.
$-v  # See $VERBOSE.
$-w  # True if option -w is set.

http://www.zenspider.com/Languages/Ruby/QuickRef.html

def handle_deflation
  case last_response["content-encoding"]
  when "gzip", "x-gzip"
    body_io = StringIO.new(last_response.body)
    last_response.body.replace Zlib::GzipReader.new(body_io).read
    last_response.delete('content-encoding')
  when "deflate"
    last_response.body.replace Zlib::Inflate.inflate(last_response.body)
    last_response.delete('content-encoding')
  end
end

dbconfig = {
  :adapter  => 'mysql2',
  :username => '',
  :password => '',
  :database => '',
  :host     => ''
}

ActiveRecord::Base.logger = Logger.new(STDOUT) #将SQL调试信息打印到标准输出
ActiveRecord::Base.establish_connection(dbconfig) #建立数据库链接,lazy模式,不使用则不主动连接

class TableNameExample < ActiveRecord::Base
  self.table_name = 'table_name_example' #此行不用写,自动生成
end

p TableNameExample.take #自然顺序第一条记录
p TableNameExample.first #按主键排序后第一条记录

tne = TableNameExample.new
tne.key1 = 'value1'
tne.key2 = 'value2'
tne.save #insert

tne = TableNameExample.find(10) #查找主键=10的记录
tne.key1 = 'value1'
tne.save #update