Code Language: text

CoffeeScript - destructuring assignment in function arguments

Language: Plain Text

printBar = ({bar}) ->
  console.log bar

printBar
  foo: 42
  bar: 'ohai destructuring args!'
Reveal More
Added 10 months ago by Segal-avatar_normal zdzolton

CoffeeScript - passing additional arguments after an anonymous function without using parens

Language: Plain Text

setTimeout ->
  # callback code here
, 2000
Reveal More
Added 10 months ago by Segal-avatar_normal zdzolton

Elide a string on word break, before a certain length

Language: Plain Text

# coffeescript

elideBefore = (text, elideLength) ->
  if text.length < elideLength then text
  else text.slice(0, elideLength).replace /\W\w*.{3}$/, '...'
Reveal More
Added 11 months ago by Segal-avatar_normal zdzolton

SPRoleDefinition

Language: Plain Text

SPRoleDefinition readDefRole = spWeb.RoleDefinitions["Read"];
SPRoleDefinition newReadDefRole = new SPRoleDefinition(readDefRole);
newReadDefRole.Name = "New Read Role";
spWeb.RoleDefinitions.Add(newReadDefRole);
Reveal More
Added 11 months ago by Sharepoint360logo_just360_normal SharePoint360

Using Seq with CoffeeScript

Language: Plain Text

# This is CoffeeScript, dammit!
# Adapted from example code here: http://is.gd/keMc3
# Big thanks to James Halliday (A.K.A. Substack)

{puts} = require 'sys'
fs = require 'fs'
{exec} = require 'child_process'
Seq = require 'seq'

Seq()
  .seq ->
    exec 'whoami', @

  .par (who) ->
    exec "groups #{who}", @

  .par (who) ->
    fs.readFile '/etc/bashrc', 'ascii', @
  
  .seq (groups, src) ->
    puts "Groups: #{groups.trim()}"
    puts "This file has #{src.length} bytes"
Reveal More
Added about 1 year ago by Segal-avatar_normal zdzolton

how to handle render :collection => @foos in rails when @foos is an empty collection

Language: Plain Text

<!-- app/views/foos/_foo.html.erb -->
<tr>
  <td>
    <%= foo.name %>
  </td>
  <td>
    <%= foo.color %>
  </td>
</tr>

<!-- app/views/foos/_foos.html.erb -->
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Color</th>
    </tr>
  </thead>
  <tbody>
    <%= render @foos %>
  </tbody>
</table>

<!-- app/views/foos/_foos_empty.html.erb -->
<p>No foos yet... <%= link_to "create some!", new_foo_path %></p>

# app/helpers/foos_helper.rb
module FoosHelper
  def render_foos
    if @foos.blank?
      render :partial => "foos_empty"
    else
      render :partial => "foos"
    end
  end
end

<!-- app/views/foos/index.html.erb -->
<%= render_foos %>
Reveal More
Added over 1 year ago by Avatar_normal nflamel

Finding out the current version of stuff installed on Heroku

Language: Plain Text

You can use the console and run system commands to get version info:

To get the version of Bundler:

heroku console %x{bundle -v}

To get the version of imagemagick:

heroku console %x{identify -version}

...etc...
Reveal More
Added almost 2 years ago by Costco_normal trevorturk

Automated Postgresql backups with pg_dump

Language: Plain Text

Annoyingly, postgres makes is very annoying to pass in your password when using pg_dump. There's a way around it, though...

PGPASSWORD=YOUR_PASS pg_dump -Fc --username=YOUR_USER --host=YOUR_HOST YOUR_DB_NAME > YOUR_BACKUP_FILE
Reveal More
Added almost 2 years ago by Costco_normal trevorturk

Search for Ruby gems on the command line

Language: Plain Text

# Just found out about this one. You can search for gems on remote sources
# (Gemcutter etc.) via the "gem search" command.
# For docs see http://docs.rubygems.org/read/chapter/10#page36

gem search -r sinatra

adamwiggins-sinatra (0.10.1)
async_sinatra (0.1.5)
BJClark-sinatra-content-for (0.2.1)
blindgaenger-sinatra-rest (0.3.3)
bmizerany-sinatra (0.9.1)
cehoffman-sinatra-respond_to (0.3.6)
christiank-sinatra-entries-visible (0.1.0)
... etc ...
Reveal More
Added almost 2 years ago by Devo_normal gbuesing

git checkout tracking branch (long and short syntax)

Language: Plain Text

Long syntax:

  git checkout --track -b branchname origin/branchname


Short syntax:

  git checkout -t origin/branchname
Reveal More
Added almost 2 years ago by Devo_normal gbuesing

starting Qmail through Xinetd

Language: Plain Text

service smtp
{
  flags = REUSE NAMEINARGS
  socket_type = stream
  protocol = tcp
  wait = no
  user = qmaild
  server = /usr/sbin/tcpd
  server_args = /var/qmail/bin/tcp-env -R /var/qmail/bin/qmail-smtpd
}
Reveal More
Added almost 2 years ago by Twitterprofilephoto_normal zh

one-liner: show info on files created during previous 72 hours.

Language: Plain Text

i=0 && while [ $i -lt 3 ]; do find -ctime $i -ls; let i=i+1; done
Reveal More
Added almost 2 years ago by Skullplainblack_normal timothyoconnell

Node.JS Image Web Server (using CoffeeScript)

Language: Plain Text

# NB: this is CoffeeScript!!
# http://jashkenas.github.com/coffee-script/
# We assume that you're storing image files in a 'test-images' directory.

sys = require 'sys'
http = require 'http'
url = require 'url'
posix = require 'posix'

workingDir: process.cwd()

getImageName: (request) ->
  requestURL = url.parse request.url
  pathSegs = requestURL.pathname.split '/'
  pathSegs.shift()
  pathSegs.shift()

http.createServer((request, response) ->
  imageName: getImageName request
  headers: { "Content-Type": "image/jpeg" }
  imagePath: workingDir + "/test-images/" + imageName
  posix.cat(imagePath, "binary").addCallback(
    (data) ->
      response.sendHeader 200, headers
      response.sendBody data, "binary"
      response.finish()
  ).addErrback(
    (err) ->
      response.sendHeader 404, headers
      response.finish()
  )
).listen 8000
Reveal More
Added about 2 years ago by Segal-avatar_normal zdzolton

Disable that annoying known_hosts checking in SSH

Language: Plain Text

Place the following in your ~/.ssh/config:

StrictHostKeyChecking no
UserKnownHostsFile /dev/null
Reveal More
Added about 2 years ago by Costco_normal trevorturk

Timezone awareness

Language: Plain Text

>> Time.zone = "EST"
=> "EST"
>> Part.first.created_at
=> Mon, 24 Aug 2009 09:26:05 EST -05:00
>> Time.zone = "Sydney"
=> "Sydney"
>> Part.first.created_at
=> Tue, 25 Aug 2009 00:26:05 EST +10:00
Reveal More
Added about 2 years ago by 22543_257969437362_629277362_4448314_4184252_n_normal eladmeidar

nginx launchd item for mac os x

Language: Plain Text

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>nginx</string>
<key>Program</key>
<string>/opt/nginx/sbin/nginx</string>
<key>KeepAlive</key>
<true/>
<key>NetworkState</key>
<true/>
<key>StandardErrorPath</key>
<string>/opt/nginx/logs/error.log</string>
<key>LaunchOnlyOnce</key>
<true/>
</dict>
</plist>
Reveal More
Added over 2 years ago by Costco_normal trevorturk

Quick and Dirty URL Validation

Language: Plain Text

# Via: http://almosteffortless.com/2009/02/09/quick-and-dirty-url-validation/

class Link < ActiveRecord::Base
 
  attr_accessible :url
  validate :validate_url
 
private
 
  def validate_url
    errors.add(:url) unless %w(200 301 302).include?(Link.status_code(self.url))
  end
 
  def self.status_code(url)
    regexp = url.match(/https?:\/\/([^\/]+)(.*)/)
    path = regexp[2].blank? ? '/' : regexp[2]
    Net::HTTP.start(regexp[1]) {|http| http.head(path).code}
  rescue
    nil
  end
 
end
Reveal More
Added over 2 years ago by Costco_normal trevorturk