Skip to content

Instantly share code, notes, and snippets.

@ilake
ilake / vim tips
Last active December 19, 2015 15:59
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" RENAME CURRENT FILE
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! RenameFile()
let old_name = expand('%')
let new_name = input('New file name: ', expand('%'), 'file')
if new_name != '' && new_name != old_name
exec ':saveas ' . new_name
exec ':silent !rm ' . old_name
redraw!
@ilake
ilake / rough_c2j.md
Created June 29, 2013 15:13
Implement Coffee2Js function to check coffee in js in realtime in vim
" Created compiled js file
function! s:Coffee2Js()
  let current_coffee = expand('%')
  let filename = substitute(expand('%:t'), ".coffee$", "", "")
  exec('! coffee -co /tmp ' . current_coffee)
  exec('split /tmp/' . filename . '.js')
endfunction
"
" Register a new command `C2J` for compile coffee to a js file
# When using a JavaScript loop to generate functions, 
# it's common to insert a closure wrapper in order to ensure that loop variables are closed over, 
# and all the generated functions don't just share the final values.
# CoffeeScript provides the do keyword,
# which immediately invokes a passed function, forwarding any arguments.

for filename in list
  do (filename) ->
    fs.readFile filename, (err, contents) ->

tonyq/website-performance/

Gem for testing

group :development, :test do
  gem 'rspec-rails', '~> 2.12.0'

  # Auto testing
  gem 'guard-rspec'
  gem 'guard-spork'
  gem 'ruby_gntp'
 gem 'rb-fsevent', '~> 0.9.1'
# Returns a hash of attributes that can be used to build a User instance
attrs = FactoryGirl.attributes_for(:user)

# Passing a block to any of the methods above will yield the return object
FactoryGirl.create(:user) do |user|
  user.posts.create(attributes_for(:post))
end
jQuery ->
$('#student_query_string').autocomplete
source: "/admin/autocomplete"
select: (event, ui)->
location.href = '/admin/students/' + ui.item.uid
open: (event, ui)->
$(".autocomplete_avatar").each (index, elem)->
$(elem).attr("src", $(elem).data('src'))
.data( "ui-autocomplete" )._renderItem = ( ul, item )->
$( "<li>" ).append( "<a><img class='autocomplete_avatar' data-src='" + item.avatar + "' src='<%= asset_path('fallback/thumb_default_avatar.png') %>'><span>" + item.name + "</span></a>" ).appendTo( ul )

fork + exec

  • exec allows you to transform the current process into any other process, and never return
  • exec doesn't close any open file descriptors or do any memory cleanup
  • All of the methods described below are base THIS theme fork + exec

Arguments to exec

  • Passing a string to exec, it will start up a shell and pass the string to shell
  • Passing a array to exec, it will skip the shell and set up the array directly as ARGV to new process
  • General you want to avoid passing a string unless you really need to. Pass an array when possible
  • running code through the shell can raise security concerns