Ruby 将数据放在OS X中的剪贴板上

IO.popen('pbcopy', 'w').print "Text to go on clipboard"

Ruby RubyCocoa控件方法为四列NSTableView

# What you NEED in the controller to be able to list someting in a NSTableView in RubyCocoa
#
# Creates the storage array, and another array which contains the names of the columns.
def initialize
  @objects = Array.new
  @columns = ["Column Name 1", "Column Name 2", "Column Name 3", "Column Name 4"]
end
	
# Returns the numbers of rows in the array for NSTableView to display.
def numberOfRowsInTableView(aTableView)
  return @objects.length
end

# Uses the @columns array to to enable a flexible number of columns when fetching data.
def tableView_objectValueForTableColumn_row(afileTable, aTableColumn, rowIndex)
  @columns.each do |column|
    if aTableColumn.headerCell.stringValue == column
	object = @objects[rowIndex]
  	return object[column]
    end
  end
end
	
# Usage:
#
# Creates the object that will go into the array. Mind you, the key values must be named the same way as the columns in the NSTableView, which is why it might be a good idea to use the actual @columns array post a identifiers.
object = {
  @columns[0] => "Put...",
  @columns[1] => "...your...",
  @columns[2] => "...values...",
  @columns[3] => "...here"
}
  
# Adds the object to the @objects array
@objects += [object]

# Updates the NSTableView after a new post/row has been added to the storage array
@tableViewOutlet.reloadData

Ruby 美味的API HTTP基本身份验证

# Creates a SSL connection with delicious API using the HTTP basic authentication.
# From http://ruby.about.com/od/tutorials/ss/delicious_tags.htm
# CHANGE USERNAME & PASS !!!

require 'net/https'
http = Net::HTTP.new('api.del.icio.us', 443)
http.use_ssl = true
http.start do |http|
	request = Net::HTTP::Get.new('/v1/tags/get')
	request.basic_auth 'username', 'password'
	response = http.request(request)
	response.value
	puts response.body
end

Ruby 使用REXML解析XML文档并打印一些文本节点

require "rexml/document"
file = File.new( "MY_FILE.xml" )
doc = REXML::Document.new file
doc.elements.each("document/record/upload_list"){
	|element| puts element.text if element.text
}

Ruby 使用TextMate的tm_dialog命令创建用户定义的菜单

SUPPORT = ENV['TM_SUPPORT_PATH'] # Retrieves the path of textmates ruby modules
DIALOG = ENV['DIALOG'] # retrieves the tm_dialog command

require SUPPORT + '/lib/escape'
require SUPPORT + '/lib/osx/plist'

# Set up your menu items
# Create an array of associative arrays that will hold your menu items
#
# NOTE: the key names of the associative array are:
# title: The menu Title
# path: The value that is associated to the selected item
# 
# When you select a menu item tm_dialog retuns both the title and path
# of the selected menu item 

items = []
items << {
  "title" => "Macs",
  "path" => "Rule"
}

items << {
  "title" => "Windows",
  "path" => "Stinks"
}

# Create a container associative array to hold your menu items
#
# transform an associative array into a plist (Property List, which is just an XML file). to_plist appears
# to be an extention that textmate loads up automatically
plist = {'menuItems' => items}.to_plist

# Run the tm_dialog command and load the result as a parsed property list.
# NOTE: e_sh is provided by the escape module.
res = OSX::PropertyList::load(`#{e_sh(DIALOG)} -up #{e_sh(plist)}`)

# simply display the returned path of the selected menu item
puts res['selectedMenuItem']['path']

Ruby Rails-GitHub-Dreamhost-Capistrano部署脚本

############################
# Based on the original DreamHost deploy.rb recipe
#
#
# GitHub settings #######################################################################################
default_run_options[:pty] = true
set :repository,  "git@github.com:username/your-repo.git" #GitHub clone URL
set :scm, "git"
set :scm_passphrase, "pa$$w0rd" #This is the passphrase for the ssh key on the server deployed to
set :branch, "master"
set :scm_verbose, true
#########################################################################################################
set :user, 'username' #Dreamhost username
set :domain, 'server.dreamhost.com'  # Dreamhost servername where your account is located 
set :project, 'projectname'  # Your application as its called in the repository
set :application, 'sub.domain.com'  # Your app's location (domain or sub-domain name as setup in panel)
set :applicationdir, "/home/#{user}/#{application}"  # The standard Dreamhost setup

# Don't change this stuff, but you may want to set shared files at the end of the file ##################
# deploy config
set :deploy_to, applicationdir
set :deploy_via, :remote_cache

# roles (servers)
role :app, domain
role :web, domain
role :db,  domain, :primary => true

namespace :deploy do
 [:start, :stop, :restart, :finalize_update, :migrate, :migrations, :cold].each do |t|
   desc "#{t} task is a no-op with mod_rails"
   task t, :roles => :app do ; end
 end
end

# additional settings
default_run_options[:pty] = true  # Forgo errors when deploying from windows
#ssh_options[:keys] = %w(/Path/To/id_rsa)            # If you are using ssh_keys
#set :chmod755, "app config db lib public vendor script script/* public/disp*"
set :use_sudo, false

#########################################################################################################

#for use with shared files (e.g. config files)
after "deploy:update_code" do
  run "ln -s #{shared_path}/database.yml #{release_path}/config"
  run "ln -s #{shared_path}/environment.rb #{release_path}/config"
end

Ruby TextMate Ruby Snippet发布

#!/usr/bin/ruby
$: << ENV['TM_SUPPORT_PATH'] + '/lib'

require "ui"
require "xmlrpc/client"

unless ENV['SNIPPLR_KEY']
  puts "Please first set your 'SNIPPLR_KEY' in Preferences > Advanced > Shell Variables."
  exit
end

unless ENV['TM_SELECTED_TEXT'] and ENV['TM_SELECTED_TEXT'].length > 0
  puts "No text selected."
  exit
end

TextMate::UI.request_string(:title => "Post Snippet to Snipplr",
                            :prompt => "Name this snippet:",
                            :button1 => "Christen") do |title|

  TextMate::UI.request_string(:title => "Post Snippet to Snipplr",
                              :prompt => "Enter tags (space-delimited):",
                              :button1 => "Post") do |tags|

    begin
      server = XMLRPC::Client.new2("http://snipplr.com/xml-rpc.php")
      snippet_list = server.call( 'snippet.post',
                                  ENV['SNIPPLR_KEY'],
                                  title,
                                  ENV['TM_SELECTED_TEXT'],
                                  tags)
      puts "Snippet successfully posted."
    rescue
      TextMate::UI.alert(:critical, "Snipplr error", "Could not post snippet.")
    end
  end
end

Ruby Camelize String

class String
  def camelize
    self.split(/[^a-z0-9]/i).map{|w| w.capitalize}.join
  end
end

Ruby 旋转日志文件

# Rotates 5 logfiles of about a meg each
config.logger = Logger.new("#{RAILS_ROOT}/log/#{ENV['RAILS_ENV']}.log", 5, 1048576)

Ruby 来自Pastie - Textmate Command

#!/usr/bin/env ruby

##Licence
# ger_from_pastie.rb - A TextMate command for getting file from http://pastie.caboo.se
#Copyright (C) 2006  Alessio Caiazza - abisso.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software Foundation,
#Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
##Installation
# for install create a new command in textmate and paste this code
# select Input: None and Output: Create New Document
# enjoy     
              

##Script
require 'net/http'

def request_document_number
  s = `\"#{ENV['TM_SUPPORT_PATH']}/bin/CocoaDialog.app/Contents/MacOS/CocoaDialog\" inputbox --title 'Get from Pastie' --informative-text 'Please enter the number of document to download' --text '' --button1 'Get' --button2 'Cancel'`
  case (a = s.split("\n"))[0].to_i
  when 1 then a[1].to_i
  when 2 then puts "Abort"; abort
  end
end  

begin                                 
  number = request_document_number 
  document = index = Net::HTTP.get_print(
    URI.parse("http://pastie.caboo.se/#{number}/download"))
rescue
  "Some error, document number may be wrong" 
end