Skip to content

Instantly share code, notes, and snippets.

@zhaocai
Created April 9, 2013 10:28
Show Gist options
  • Save zhaocai/5344692 to your computer and use it in GitHub Desktop.
Save zhaocai/5344692 to your computer and use it in GitHub Desktop.
# #!/usr/bin/ruby
# -- env -- [[[1 -------------------------------------------------------------
snippet shebang
abbr #!/usr/bin/env ruby
prev_word '^'
#!/usr/bin/env ${1:ruby}
# -*- coding: utf-8 -*-
${2}
snippet modeline
abbr ~~ modeline ~~
prev_word '^'
${0}
# ~~ modeline ~~ [[[1 -----------------------------------------------------
#
# vim: set ft=ruby ts=2 sw=2 tw=78 fdm=marker fmr=[[[,]]] fdl=1 :
snippet LOAD_PATH
abbr $LOAD_PATH << ...
prev_word '^'
($LOAD_PATH << File.expand_path("${1:..}", __FILE__)).uniq!
${0}
snippet req
options head
require "${1}"${2}
# -- library -- [[[1 ---------------------------------------------------------
snippet common_require
prev_word '^'
require 'rubygems' unless defined? Gem # rubygems is only needed in 1.8
require 'awesome_print'
require 'zucker/debug'
${0}
# -- class -- [[[1 -----------------------------------------------------------
# attr_reader
snippet attr_reader
attr_reader :${1:attr_names}
# attr_writer
snippet attr_writer
attr_writer :${1:attr_names}
# attr_accessor
snippet attr_accessor
attr_accessor :${1:attr_names}
snippet class
class ${1:`necosnip#ruby_class_name_from_filename()`} ${2:#:< ParentClass}
def initialize(${3:args})
${4}
end
end
snippet module_include
module ${1:`substitute(Filename(), '^.', '\u&', '')`}
module ClassMethods
${2}
end
module InstanceMethods
end
def self.included(receiver)
receiver.extend ClassMethods
receiver.send :include, InstanceMethods
end
end
# include Enumerable
snippet Enum
include Enumerable
def each(&block)
${1}
end
# include Comparable
snippet Comparable
include Comparable
def <=>(other)
${1}
end
# extend Forwardable
snippet Forwardable
extend Forwardable
snippet def_test
def test_${1:case_name}
${2:TARGET}
end
# def self
snippet defs
def self.${1:class_method_name}
${2:TARGET}
end
# def method_missing
snippet def_method_missing
def method_missing(meth, *args, &blk)
${1}
end
snippet def_delegator
def_delegator :${1:@del_obj}, :${2:del_meth}, :${3:new_name}
snippet def_delegators
def_delegators :${1:@del_obj}, :${2:del_methods}
snippet alias_method
alias_method :${1:new_name}, :${2:old_name}
snippet app
if __FILE__ == $PROGRAM_NAME
${1}
end
snippet array
Array.new(${1:10}) { |${2:i}| ${3} }
snippet hash
Hash.new { |${1:hash}, ${2:key}| $1[$2] = ${3} }
snippet file_foreach
abbr File.foreach() { |line| .. }
File.foreach(${1:"path/to/file"}) { |${2:line}| ${3} }
snippet dir_glob
Dir.glob(${1:"dir/glob/*"}) { |${2:file}| ${3} }
snippet Dir
abbr Dir[".."]
Dir[${1:"glob/**/*.rb"}]${2}
snippet delete_if
delete_if { |${1:e}| ${2} }
snippet fill
fill(${1:range}) { |${2:i}| ${3} }
# flatten_once()
snippet flatten_once
inject(Array.new) { |${1:arr}, ${2:a}| $1.push(*$2)}${3}
snippet zip
zip(${1:enums}) { |${2:row}| ${3} }
# downto(0) { |n| .. }
snippet down_to
downto(${1:0}) { |${2:n}| ${3} }
snippet step
step(${1:2}) { |${2:n}| ${3} }
snippet times
times { |${1:n}| ${2} }
snippet upto
upto(${1:1.0/0.0}) { |${2:n}| ${3} }
# -- condition -- [[[1 -------------------------------------------------------
snippet case
case ${1:object}
when ${2:condition}
${3}
else
${4}
end
snippet when
when ${1:condition}
${2:TARGET}
snippet ife
if ${1:condition}
${2:TARGET}
else
${3}
end
snippet elsif
elsif ${1:condition}
${2:TARGET}
snippet unless
unless ${1:condition}
${2:TARGET}
end
snippet edn
abbr => end?
end
# -- debug -- [[[1 -----------------------------------------------------------
snippet pry
abbr binding.pry
prev_word '^'
binding.pry
${0}
snippet backtrace
prev_word '^'
puts "[ backtrace ]\n #{caller.join("\n ")}"
${0}
snippet logger
abbr Logging.logger
options head
require 'logging'
logger = Logging.logger['${1:Default}']
logger_file = File.join(Dir.home, '${2:Library/Logs/logging/default.log}')
logger.add_appenders(
Logging.appenders.stdout,
Logging.appenders.file(logger_file)
)
logger.level = :debug
${0}
# -- exception -- [[[1 -------------------------------------------------------
snippet rescue
alias try
abbr begin ... rescue ... end
prev_word '^'
begin
${1:TARGET}
rescue ${2:} => ${3:e}
raise ${3}
end
# -- file -- [[[1 -----------------------------------------------------------
snippet fileExpandPath
alias fep
options word
File.expand_path(${1:TARGET})
# -- loop -- [[[1 ------------------------------------------------------------
snippet for
options head
for i in ${1:1}..${2:5}
${2:TARGET}
end
snippet dovar
abbr do |var| end
do |${1:var}|
${2}
end
snippet blockValue
alias bV
abbr { |v| }
{ |${1:v}| ${2} }
snippet blockKeyValue
alias bKV
abbr { |k, v| }
{ |${1:k, v}|
${2}
}
snippet while
while ${1:condition}
${2}
end
snippet until
until ${1:condition}
${2}
end
snippet each
options word
each { |${1:k}|
${2:TARGET}
}
snippet each_byte
options word
each_byte { |${1:byte}| ${2} }
snippet each_char
options word
each_char { |${1:chr}| ${2} }
snippet each_cons
options word
each_cons(${1:2}) { |${2:group}| ${3} }
snippet each_index
options word
each_index { |${1:i}| ${2} }
snippet each_key
options word
each_key { |${1:key}| ${2} }
snippet each_line
options word
each_line { |${1:line}| ${2} }
snippet each_pair
options word
each_pair { |${1:name}, ${2:val}| ${3} }
snippet each_slice
options word
each_slice(${1:2}) { |${2:group}| ${3} }
snippet each_value
options word
each_value { |${1:val}| ${2} }
snippet each_with_index
options word
each_with_index { |${1:e}, ${2:i}| ${3} }
snippet reverse_each
options word
reverse_each { |${1:e}| ${2} }
snippet inject
options word
inject(${1:init}) { |${2:mem}, ${3:var}| ${4} }
snippet map
options word
map { |${1:e}| ${2} }
snippet map_with_index
options word
enum_with_index.map { |${1:e}, ${2:i}| ${3} }
snippet sort
options word
sort { |a, b| ${1} }
snippet sort_by
options word
sort_by { |${1:e}| ${2} }
snippet random_sort
options word
sort_by { rand }
snippet all
all? { |${1:e}| ${2} }
snippet any
any? { |${1:e}| ${2} }
snippet cl
classify { |${1:e}| ${2} }
snippet col
collect { |${1:e}| ${2} }
snippet det
detect { |${1:e}| ${2} }
snippet fet
fetch(${1:name}) { |${2:key}| ${3} }
snippet fin
find { |${1:e}| ${2} }
snippet fina
find_all { |${1:e}| ${2} }
snippet gre
grep(${1:/pattern/}) { |${2:match}| ${3} }
snippet sub
${1:g}sub(${2:/pattern/}) { |${3:match}| ${4} }
snippet sca
scan(${1:/pattern/}) { |${2:match}| ${3} }
snippet max
max { |a, b|, ${1} }
snippet min
min { |a, b|, ${1} }
snippet par
partition { |${1:e}|, ${2} }
snippet rej
reject { |${1:e}|, ${2} }
snippet sel
select { |${1:e}|, ${2} }
snippet lam
lambda { |${1:args}| ${2} }
snippet do
do |${1:variable}|
${2}
end
snippet key_sym
:${1:key} => ${2:"value"}${3}
snippet open
open(${1:"path/or/url/or/pipe"}, "${2:w}") { |${3:io}| ${4} }
# path_from_here()
snippet path_from_here
File.join(File.dirname(__FILE__), *%2[${1:rel path here}])${2}
# option_parse {}
snippet optparse
require "optparse"
options = {${1:default => "args"}}
ARGV.options do |opts|
opts.banner = \"Usage: #{File.basename($PROGRAM_NAME)}
snippet opt
opts.on( "-${1:o}", "--${2:long-option-name}", ${3:String},
"${4:Option description.}") do |${5:opt}|
${6}
end
snippet flunk
flunk("${1:Failure message.}")${2}
# Benchmark.bmbm do .. end
snippet benchmark
TESTS = ${1:10_000}
Benchmark.bmbm do |results|
${2}
end
snippet report
results.report("${1:name}:") { TESTS.times { ${2} }}
# deep_copy(..)
snippet deep_copy
Marshal.load(Marshal.dump(${1:obj_to_copy}))${2}
snippet PStore
PStore.new(${1:"file_name.pstore"})${2}
# xmlread(..)
snippet xml_load
REXML::Document.new(File.read(${1:"path/to/file"}))${2}
# xpath(..) { .. }
snippet xpath
elements.each(${1:"//Xpath"}) do |${2:node}|
${3}
end
# class_from_name()
snippet class_from_name
split("::").inject(Object) { |par, const| par.const_get(const) }
snippet namespace
namespace :${1:`expand("%:p:h:t:r")`} do
${2}
end
snippet task
desc "${1:Task description}"
task :${2:task_name} => [${3:#::dependent, :tasks}] do
${4}
end
# -- io -- [[[1 -----------------------------------------------------------
snippet yamlLoad
alias yL
options head
YAML::load( File.read(${1:TARGET}) )
snippet yamlDump
alias yD
options head
File.open(${1:TARGET}, "wb") { |f| YAML::dump(${2:object}, f)}
snippet plistLoad
alias pL
options head
Plist::parse_xml( File.read(${1:TARGET}) )
snippet plistDump
alias pD
options head
File.open(${1:TARGET}, "wb") { |f| f.puts ${2:object}.to_plist}
snippet marshalLoad
alias mL
options head
File.open(${1:TARGET}, "rb") { |f| Marshal.load(f) }
snippet marshalDump
alias mD
options head
File.open(${1:TARGET}, "wb") { |f| Marshal::dump(${2:object}, f)}
# -- shell -- [[[1 -----------------------------------------------------------
snippet xShell
abbr %x{...}
%x{${1:#shell code}}
# unix_filter {}
snippet argf
ARGF.each_line${1} do |${2:line}|
${3}
end
# -- string -- [[[1 ----------------------------------------------------------
snippet hereDoc
prev_word '^'
${1:var} = <<-${2:__DOC__}
${3:TARGET}
${2}
snippet applescript
abbr osascript
options head
%x{osascript <<__APPLESCRIPT__
${1:TARGET}
__APPLESCRIPT__}
# ~~ modeline ~~ [[[1 --------------------------------------------------------
#
# vim: set ft=snippet ts=2 sw=2 sts=2 tw=78 fdm=marker fmr=[[[,]]] fdl=1 :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment