<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Nicwilliams's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 05 Aug 2008 21:34:51 GMT</pubDate>
    <description>DZone Snippets: Nicwilliams's Code Snippets</description>
    <item>
      <title>Pretty print in Nu</title>
      <link>http://snippets.dzone.com/posts/show/5782</link>
      <description>I miss the 'pp' helper method in Ruby. Fortunately Nu has "-description" method on objects, which is like a #to_pretty_print method in Ruby. So, implementing "pp" takes one line of code:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(function pp (obj) (puts (obj description)))&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And for a demonstration:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ nush&lt;br /&gt;Nu Shell.&lt;br /&gt;% (function pp (obj) (puts (obj description)))&lt;br /&gt;(do (obj) ((puts (obj description))))&lt;br /&gt;% (pp (array 1 2 3 (array "a" "b" 10)))&lt;br /&gt;(&lt;br /&gt;    1,&lt;br /&gt;    2,&lt;br /&gt;    3,&lt;br /&gt;        (&lt;br /&gt;        a,&lt;br /&gt;        b,&lt;br /&gt;        10&lt;br /&gt;    )&lt;br /&gt;)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Thanks to Patrick Thomson for this!</description>
      <pubDate>Fri, 18 Jul 2008 00:27:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5782</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Exploring context and globals with Nu</title>
      <link>http://snippets.dzone.com/posts/show/5781</link>
      <description>From Tim in mailing list:&lt;br /&gt;&lt;br /&gt;Global name definitions are put into the symbol table; that is&lt;br /&gt;slightly more efficient than normal assignments (such as those made&lt;br /&gt;with the set, macro, and function operators), which are kept in&lt;br /&gt;NSDictionaries.  It also makes them easier to use in Nu code that is&lt;br /&gt;called from Objective-C without specifying a context, which you can do&lt;br /&gt;by sending the evalWithContext: message to a parsed code object with a&lt;br /&gt;nil argument. Usually that happens in Objective-C, but since you can&lt;br /&gt;send nearly any ObjC message from Nu, here's a little demo in nush&lt;br /&gt;[ed: I expanded it to show more information]:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ nush&lt;br /&gt;Nu Shell.&lt;br /&gt;% (parse "(puts 22)")&lt;br /&gt;(progn (puts 22))&lt;br /&gt;% (set prog (parse "(puts 22)"))&lt;br /&gt;(progn (puts 22))&lt;br /&gt;% (prog class)&lt;br /&gt;NuCell&lt;br /&gt;% (set progx (parse "(puts x)"))&lt;br /&gt;(progn (puts x))&lt;br /&gt;% (progx class)&lt;br /&gt;NuCell&lt;br /&gt;% (set x 23)&lt;br /&gt;23&lt;br /&gt;% (x class)&lt;br /&gt;NSCFNumber&lt;br /&gt;% (eval progx)&lt;br /&gt;23&lt;br /&gt;()&lt;br /&gt;% (prog evalWithContext: nil)&lt;br /&gt;22&lt;br /&gt;()&lt;br /&gt;% (progx evalWithContext: nil)&lt;br /&gt;NuUndefinedSymbol: undefined symbol: x&lt;br /&gt;% (progx evalWithContext: (context))&lt;br /&gt;23&lt;br /&gt;()&lt;br /&gt;% (global x 50)&lt;br /&gt;50&lt;br /&gt;% (progx evalWithContext: (context))&lt;br /&gt;23&lt;br /&gt;()&lt;br /&gt;% (progx evalWithContext: nil)&lt;br /&gt;50&lt;br /&gt;()&lt;/code&gt;</description>
      <pubDate>Fri, 18 Jul 2008 00:19:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5781</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Find ports used by applications on Mac OS X (i.e. netstat for os x)</title>
      <link>http://snippets.dzone.com/posts/show/5712</link>
      <description>&lt;code&gt;netstat&lt;/code&gt; on os x doesn't do what you hope it does - show port numbers for applications.&lt;br /&gt;&lt;br /&gt;Here's a snippet for your .bash_profile:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# replacement netstat cmd to find ports used by apps on OS X&lt;br /&gt;alias netstat_osx="sudo lsof -i -P"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;(thx Lachie Cox)</description>
      <pubDate>Mon, 30 Jun 2008 00:06:33 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5712</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Convert cp1252-&gt; utf-8 character set (python and ruby)</title>
      <link>http://snippets.dzone.com/posts/show/5367</link>
      <description>Oooh, I hate character sets. Specifically that there are more than one of them. Here is a Ruby version of a Python script I found to convert cp1252 (aka windows-1252) into utf-8.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def clean_up dirty_text&lt;br /&gt;    newstr = ""&lt;br /&gt;    dirty_text.length.times do |i|&lt;br /&gt;      character = dirty_text[i]&lt;br /&gt;      newstr += if character &lt; 0x80&lt;br /&gt;        character.chr&lt;br /&gt;      elsif character &lt; 0xC0&lt;br /&gt;        "\xC2" + character.chr&lt;br /&gt;      else&lt;br /&gt;        "\xC3" + (character - 64).chr&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    newstr&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The original Python script was (http://miscoranda.com/96):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/python&lt;br /&gt;import sys&lt;br /&gt;for c in sys.stdin.read(): &lt;br /&gt;   if ord(c) &lt; 0x80: sys.stdout.write(c)&lt;br /&gt;   elif ord(c) &lt; 0xC0: sys.stdout.write('\xC2' + c)&lt;br /&gt;   else: sys.stdout.write('\xC3' + chr(ord(c) - 64))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 16 Apr 2008 11:39:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5367</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Array#pad!</title>
      <link>http://snippets.dzone.com/posts/show/5331</link>
      <description>My ri docco says there is an Array#pad! but runtime Ruby says there isn't. Bah. So I wrote one:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;  def pad!(expected_length, pad_item = nil)&lt;br /&gt;    while expected_length &gt; length&lt;br /&gt;      self &lt;&lt; pad_item&lt;br /&gt;    end&lt;br /&gt;    self&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;</description>
      <pubDate>Mon, 07 Apr 2008 10:44:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5331</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>TextMate snippet for load_model methods in Rails controllers</title>
      <link>http://snippets.dzone.com/posts/show/4952</link>
      <description>With nested routes, I find I create a lot of controller methods like:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def load_user&lt;br /&gt;  @user = User.find(params[:user_id]) if params[:user_id]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here's a TextMate snippet, so you can just type: defmodel, TAB, user, TAB, and you're done.&lt;br /&gt;&lt;br /&gt;Snippet:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def load_${1:model}&lt;br /&gt;	@$1 = ${1/[[:alpha:]]+|(_)/(?1::\u$0)/g}.find(params[${2::$1_}id])${3: if params[:$1_id]}&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Activation: defmodel&lt;br /&gt;Scope: source.ruby.rails &lt;br /&gt;&lt;br /&gt;I have this in the Ruby on Rails bundle, but you can put it anywhere. Its the scope that is important.</description>
      <pubDate>Mon, 07 Jan 2008 00:26:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4952</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Upgrade rubygems and/or specific gems themselves via capistrano</title>
      <link>http://snippets.dzone.com/posts/show/4905</link>
      <description>When a latest RubyGems is released (e.g. 0.9.5 recently) or Rails (e.g. 2.0.2) you might want to push those upgrades to all your production machines.&lt;br /&gt;&lt;br /&gt;Add these two tasks to your deploy.rb capistrano file (add namespaces for cap2.0 if you like)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  desc "Updates RubyGems version"&lt;br /&gt;  task :gem_update_system do&lt;br /&gt;    sudo "gem update --system"&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;  desc "Install a RubyGem from remote source"&lt;br /&gt;  task :gem_install do&lt;br /&gt;    puts "USAGE: GEM=gemname cap gems_install" and next unless ENV['GEM']&lt;br /&gt;    sudo "gem install #{ENV['GEM']} --no-ri --no-rdoc"&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To upgrade rubygems and rails, for example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;cap gem_update_system&lt;br /&gt;GEM=rails cap gem_install&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 19 Dec 2007 22:10:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4905</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Checkout a Git clone of an SVN repository in parent folder</title>
      <link>http://snippets.dzone.com/posts/show/4813</link>
      <description>Here is a quick executable cmd that you run inside a folder checked out with Subversion. It will clone the repository using Git into the parent folder as &amp;lt;projname&amp;gt;.git&lt;br /&gt;&lt;br /&gt;Stick this code into ~/bin/gitify and add ~/bin to your path:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby -wKU&lt;br /&gt;&lt;br /&gt;# get svn info location&lt;br /&gt;svnurl = `svn info | grep "^URL:"`.gsub('URL: ','').chomp&lt;br /&gt;&lt;br /&gt;# project = basename&lt;br /&gt;project = File.basename(Dir.pwd)&lt;br /&gt;&lt;br /&gt;puts cmd = "git-svn clone #{svnurl} ../#{project}.git"&lt;br /&gt;&lt;br /&gt;`#{cmd}`&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 22 Nov 2007 22:35:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4813</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Auto-populate socket value in rails database.yml using TextMate snippet</title>
      <link>http://snippets.dzone.com/posts/show/4302</link>
      <description>&lt;br /&gt;When using mysql for rails apps, you may need a &lt;code&gt;socket:&lt;/code&gt; value. I can never remember mine. So I added a TextMate snippet to find it. (read below for non-TextMate)&lt;br /&gt;&lt;br /&gt;Snippet text:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;socket: `mysql_config --socket`&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Activation: &lt;code&gt;socket:&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Scope selector: &lt;code&gt;source.yaml - string&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Usage:&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;In your database.yml, go to the line &lt;code&gt;socket: &lt;/code&gt;, delete any blank spaces til the cursor is at the colon, then press TAB and wait. The line will be updated with the socket location.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Non-TextMate&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;If you don't have textmate, you can get your socket location using:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;mysql_config --socket&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And paste the result into your database.yml&lt;br /&gt;</description>
      <pubDate>Mon, 16 Jul 2007 07:30:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4302</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Dump postgres production data into development database</title>
      <link>http://snippets.dzone.com/posts/show/3958</link>
      <description>When bug requests come in, its great to grab a copy of the current production (or system test env) data and sync it into your development database.&lt;br /&gt;&lt;br /&gt;Here's a capistrano recipe for postgresql:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc "Dumps target database into development db"&lt;br /&gt;task :sync_db do&lt;br /&gt;  env   = ENV['RAILS_ENV'] || ENV['DB'] || 'production'&lt;br /&gt;  file  = "#{application}.sql.bz2"&lt;br /&gt;  remote_file = "#{shared}/log/#{file}"&lt;br /&gt;  run "pg_dump --clean --no-owner --no-privileges -U#{db_user} -h#{db_host} #{db_name}_#{env} | bzip2 &gt; #{file}" do |ch, stream, out|&lt;br /&gt;    ch.send_data "#{db_password}\n" if out =~ /^Password:/&lt;br /&gt;    puts out&lt;br /&gt;  end&lt;br /&gt;  puts rsync = "rsync #{user}@#{domain}:#{file} tmp"&lt;br /&gt;  `#{rsync}`&lt;br /&gt;  puts depackage = "bzcat tmp/#{file} | psql #{local_db_dev}"&lt;br /&gt;  `#{depackage}`&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 06 May 2007 09:00:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3958</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
  </channel>
</rss>
