Skip to content

Instantly share code, notes, and snippets.

View benwoodward's full-sized avatar
💭
Working on spoken.app

Ben Woodward benwoodward

💭
Working on spoken.app
View GitHub Profile
@benwoodward
benwoodward / word_wrap.rb
Created March 22, 2012 16:36
Split long words every 10 characters
s = 'supercalifragelisticexpealodocious'
s.scan(/\S{10}/).join('-\n')
# => "supercalif-\\nragelistic-\\nexpealodoc"
@benwoodward
benwoodward / .editrc
Created March 22, 2012 23:05
vi-mode in irb / rails console
# cat .editrc bind -v
bind -v
@benwoodward
benwoodward / .vimrc
Last active October 4, 2015 04:28
Vim shortcut to emove trailing whitespace
" remove trailing whitespace (don't use on binary files!!)
map <leader>tws :%s/\s\+$//<CR>
" go to previous file
map <Leader>p <C-^>
@benwoodward
benwoodward / .gitconfig
Created May 9, 2012 09:21
Working setup for p4merge with git 1.7 upwards.
[merge]
tool = p4merge
keepBackup = false
[mergetool "p4merge"]
cmd = p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
keepTemporaries = false
trustExitCode = false
keepBackup = false
[diff]
external = p4diff
@benwoodward
benwoodward / irb
Created May 9, 2012 12:21
Find method definition in Ruby
p ModelName.method(:method_name).source_location
@benwoodward
benwoodward / _index.html.haml
Created August 25, 2012 12:59
Ruby on Rails Sortable/Reorderable lists on STI models using ranked_model, Twitter Bootstrap and inherited_resources
%h1= "Listing " + "#{resource_name}s"
.btn-group.pull-right
= link_to "All #{resource_name(item: resource_class.superclass)}s", polymorphic_path([:admin, resource_class.superclass]), class: 'btn'
= link_to "Add #{resource_name}", new_resource_path, class: 'btn-primary btn'
%br
%br
%table.table.table-bordered.table-striped#sortable{:data => {update_url: polymorphic_path([:sort, :admin, resource_class])}}
@benwoodward
benwoodward / vim_tips.md
Created August 30, 2012 23:20
Vim commands worth remembering ..

change / delete to a character

ct[char] # e.g. ct" to delete to the next " and go into insert mode dt[char] # dt" to delete up to next "

change inner-word, delineate with "

ci"

delete after-word, delineate with parens

da)

Some useful references;

@benwoodward
benwoodward / list.rb
Created October 1, 2012 15:21
An expensive transaction in acts_as_list
# This has the effect of moving all the lower items up one.
def decrement_positions_on_lower_items(position=nil)
return unless in_list?
position ||= send(position_column).to_i
acts_as_list_class.unscoped.update_all(
"#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} > #{position}"
)
end
@benwoodward
benwoodward / gist:4539890
Last active December 11, 2015 03:48
Useful Git snippets

Edit historical commit

git-checkout 0f0d8a27622e7bf7f008983c4b8ee23bfb9843ab
editor path/to/file
git-add path/to/file
git-commit --amend -v
git-rebase --onto HEAD 0f0d8a27622e7bf7f008983c4b8ee23bfb9843ab master
@benwoodward
benwoodward / .irbrc
Created February 14, 2013 11:41
Locate and open (in TextMate) at location of method source. http://pragmaticstudio.com/blog/2013/2/13/view-source-ruby-methods
# source_for(Person.new, :update)
def source_for(object, method)
location = object.method(method).source_location
`mate #{location[0]} -l #{location[1]}`
location
end