sonia hamilton – life on the digital bikepath – sonia@snowfrog.net

10 October 2009

Ruby, Struct, OpenStruct, hashes

Filed under: Ruby — Sonia @ 16:57

An interesting Ruby example of an easy way to setup classes that are really just for passing around data using Struct – essentially a neat wrapper around a hash of values:

class Node < Struct.new( :level, :tag, :data, :refid )
  def initialize( line=nil )
    ...various mungings of line omitted...
    super level, tag.downcase, data, refid
  end
end

Now I can do stuff like:

node = Node.new( line )
print node.level

rather than

node = {}
...various munging of data into node hash...
print node[:value]

Not earth-shattering, but nice. See also ri Struct, ri OpenStruct. From Pragmattic Programmers’ Best of Ruby Quiz

2 October 2009

Ruby ARGV, options and OptionParser

Filed under: Ruby — Sonia @ 14:25

A nice Ruby example (for my reference) showing how to parse commandline options; the results are put in a hash called options (created on line 3).

Interestingly, the ARGV array is automatically extended to include the options method even before require ‘optparse’ is run. See also ri OptionParser and stderr.org. From Pragmattic Programmers’ Best of Ruby Quiz:

if __FILE__ == $0
  require 'optparse'
  options = { :size => 2 }
  number = ARGV.pop

  ARGV.options do |opts|
    script_name = File.basename($0)
    opts.banner = "Usage: ruby #{script_name} [options] number"

    opts.separator ""

    opts.on("-s", "--size size", Numeric,
      "Specify the size of line segments.",
      "Default: 2"
    ) { |options[:size]| }

    opts.separator ""

    opts.on("-h", "--help", "Show this help message.") { puts opts; exit }

    opts.parse!
  end

end

1 October 2009

Ruby here document example

Filed under: Ruby — Sonia @ 20:50

A nice ruby example (for my reference) of using here documents to load data/logic. From Pragmattic Programmers’ Best of Ruby Quiz:

DIGITS = <<END_DIGITS.split("\n").map { |row| row.split(" # ") }.transpose
 -  #     #  -  #  -  #     #  -  #  -  #  -  #  -  #  -
| | #   | #   | #   | # | | # |   # |   #   | # | | # | |
    #     #  -  #  -  #  -  #  -  #  -  #     #  -  #  -
| | #   | # |   #   | #   | #   | # | | #   | # | | #   |
 -  #     #  -  #  -  #     #  -  #  -  #     #  -  #  -
END_DIGITS

Ruby Test::Unit Assertions

Filed under: Ruby — Sonia @ 19:25

A good summary (for my reference) of the assertions that can be used with Test::Unit (from Pragmatic Programmers’ Programming Ruby).

test unit assertions

15 September 2009

Ruby rcov rexml error

Filed under: Ruby — Sonia @ 19:30

Ruby’s rcov had a workaround for a breakage in the rexml library, but the workaround broke. This causes no implicit conversion from nil to integer on stderr. Quick solution:

rcov-0.8.1.2.0/lib/rcov/report.rb
-if RUBY_VERSION == "1.8.6" && defined? REXML::Formatters::Transitive
+if ( RUBY_VERSION == "1.8.6" || RUBY_VERSION == "1.8.7" ) && defined? REXML::Formatters::Transitive

Versions:

% cat /etc/issue.net
Ubuntu 9.04
% ruby --version
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
% gem list --local rcov
rcov (0.8.1.2.0)

28 August 2009

git branch management scripts

Filed under: Git — Sonia @ 10:44

A couple of scripts I (very) quickly wrote for managing git branches, so I don’t have to keep reading git cheat. No error checking or intelligence in the scripts – YMMV.

Delete a tracking branch both locally and remotely:

% cat gitkillbranch
#!/bin/bash

asksure() {
echo -n "Are you sure (Y/N)? "
while read -r -n 1 -s answer; do
 if [[ $answer = [YyNn] ]]; then
 [[ $answer = [Yy] ]] && retval=0
 [[ $answer = [Nn] ]] && retval=1
 break
 fi
done
echo
return $retval
}

echo
echo "*********************************************************"
echo "This will delete the branch $1 both locally and remotely."
echo "*********************************************************"
echo

if asksure; then
 echo "sure"
 git br -d $1
 git br -r -d origin/${1}
 git push origin :${1}
else
 echo "Not deleting"
fi

Track all remote branches locally:

% cat gittrackall
#!/usr/bin/ruby

result = `git br -r`
result.each do |line|
 line.chomp!
 branch = line.split('/')[1]
 next if branch =~ /HEAD|master/
 puts branch
 `git branch --track #{branch} origin/#{branch}`
end

17 August 2009

Installing Aptana + RadRails on Ubuntu Jaunty

Filed under: Ruby on Rails, Ubuntu — Sonia @ 11:36

I’ve always used vim as my main editor, but I thought I’d try RadRails since so many Rails people rave about it. Installing it on Ubuntu Jaunty is a bit non-obvious:

  • sudo aptitude install openjdk-6-jre (or your preferred jre)
  • download and unpack aptana zip to ~/.aptana
  • download and unpack xulrunner to ~/.xulrunner
  • create a script like ~/bin/aptana:
#!/bin/bash
MOZILLA_FIVE_HOME=~/.xulrunner
if [ $LD_LIBRARY_PATH ]; then
    LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME:$LD_LIBRARY_PATH
else
    LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME
fi
export MOZILLA_FIVE_HOME LD_LIBRARY_PATH
~/.aptana/AptanaStudio -vm /usr/lib/jvm/java-6-openjdk/bin

[ubuntu] Installing Aptana on Jaunty – Ubuntu Forums

7 August 2009

Ubuntu and Toshiba Satellite Pro L300 fan

Filed under: Ubuntu — Sonia @ 19:23

I recently bought a new laptop – a bottom of the range Toshiba Satellite Pro L300 costing $780 (extra RAM pushed the price up). I used to buy high-end laptops but my thinking was changed when my latest one got stolen and I started using an old one I’d (literally) dredged out of a rubbish bin. I mostly use shell/vi/ssh, Thunderbird and Firefox, don’t carry my laptop around with me, and all my stuff is backed up across multiple machines using git, so why shell out the big bucks?

So far I’m liking the Satellite Pro. Ubuntu 9.04 installed without a hitch, sound and wireless worked straight out of the box, the keyboard has a nice feel to it and all the keys are in the proper places (unlike Dell laptops that seem to scatter them all over the place).

Only problem was the fan. The poor little cpu is a bit underpowered so the cpu load average continually sits on or above 1, and the fan was going mental. A quick search on Google found the solution: in /boot/grub/menu.lst change defoptions to

# defoptions=acpi_osi="Linux"

run sudo update-grub, reboot.

27 July 2009

Troubleshooting Sound on Ubuntu

Filed under: Audio, Ubuntu — Sonia @ 21:36

Some good documentation on Troubleshooting Sound on Ubuntu.

And a nice post on getting Amarok going on Jaunty – remove phonon-backend-gstreamer and install phonon-backend-xine (bug 349847). I can now go back to grooving to some laid-back Brazilian Reggae – Tribo de Jah – Ruinas de Babilonia:

Veja a face sofrida dessa gente, Tanta gente sofrida,
Buscando uma vida decente, Buscando um pouco de paz em suas vidas…

24 July 2009

git – ignore vim temporary files in all directories

Filed under: Uncategorized — Sonia @ 12:42

Up until a few weeks ago I was using subversion for all my personal stuff. But then I got fredded when my free subversion hosting (xp-dev.com) went down for a few days, so I’ve moved to git. Yay, no more central repository to go down…

To ignore files in git you use .gitignore, eg for a rails project:

% cat .gitignore
log/*.log
tmp/**/*

But how to ignore files in all directories? Use .git/info/exclude. For example, to ignore all temporary files generated by vim:

% cat .git/info/exclude
.*.sw*

All the howto’s I’ve read so far mention .gitignore but not .git/info/exclude – I stumbled across this by accident. More RTFM’ing to do…

Correction

To ignore all files in all directories, .gitignore can be used. A bit of experimentation and reading of man gitignore shows that any wildcard that doesn’t contain a / will apply to that directory and all child directories.

.gitignore is propagated during clone operations, whereas .git/info/exclude isn’t ie use the former for common settings and the latter for personal settings.

From the manpage:

Patterns which should be version-controlled and distributed to other repositories via clone (i.e., files that all developers will want to ignore) should go into a .gitignore file. Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user´s workflow) should go into the $GIT_DIR/info/exclude file. Patterns which a user wants git to ignore in all situations (e.g., backup or temporary files generated by the user´s editor of choice) generally go into a file specified by core.excludesfile in the user´s ~/.gitconfig.

« Newer PostsOlder Posts »

Blog at WordPress.com.