Re: CoffeeScript - one-liner try-catch
Refactoring of: CoffeeScript - one-liner try-catch
Language: Plain Text
# the trick is using the `then` keyword
try fail() catch e then console.log e
Reveal More
Refactoring of: CoffeeScript - one-liner try-catch
Language: Plain Text
# the trick is using the `then` keyword
try fail() catch e then console.log e
Language: Plain Text
printBar = ({bar}) ->
console.log bar
printBar
foo: 42
bar: 'ohai destructuring args!'
Language: Plain Text
try fail() catch e then console.log e
Language: Plain Text
setTimeout ->
# callback code here
, 2000
Language: Plain Text
# coffeescript
elideBefore = (text, elideLength) ->
if text.length < elideLength then text
else text.slice(0, elideLength).replace /\W\w*.{3}$/, '...'
Language: Plain Text
SPRoleDefinition readDefRole = spWeb.RoleDefinitions["Read"];
SPRoleDefinition newReadDefRole = new SPRoleDefinition(readDefRole);
newReadDefRole.Name = "New Read Role";
spWeb.RoleDefinitions.Add(newReadDefRole);
SharePoint360
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"
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 %>
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...
trevorturk
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
trevorturk
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 ...
Language: Plain Text
Long syntax:
git checkout --track -b branchname origin/branchname
Short syntax:
git checkout -t origin/branchname
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
}
Language: Plain Text
i=0 && while [ $i -lt 3 ]; do find -ctime $i -ls; let i=i+1; done
timothyoconnell
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
Language: Plain Text
Place the following in your ~/.ssh/config:
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
trevorturk
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
eladmeidar
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>
trevorturk
Language: Plain Text
puts "satisfaction"
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
trevorturk