Tuesday, June 21, 2011

Testing Ruby Scripts Without .rb Extension

Say you you want to test functions in a ruby script that doesn't have a ".rb" extension. Below are 2 tips.

To start writing you test you must use 'load' and NOT 'require.' Require is to load libraries (.so, .dll, etc) and will search for a file with a .rb extension. Load on the other hand loads ruby scripts and does not require a .rb extension. You will actually get an error if you use require saying "can't find file blah_blah"

Matz does a better job explaining the differences between the two:

"require is to load "libraries", where load is to load "scripts". The
target of the load method might be configuration files, or anything
else from the specified path, but at least they are all Ruby scripts.
On the other hand, the require method loads Ruby libraries or dynamic
loading C implemented libraries reside in the load path. So it needs
file path extension to distinguish which kind of library to read."

The above was taken from here.

Once the load fires your script will be executed immediately! To stop that from happening wrap the entry point of your ruby script with "if $0 == __FILE__…end" This says, if the current file (__FILE__) is equal to the running program ($0) then execute this block. This still allows you to execute the script with a direct call.

EOL

Tuesday, May 24, 2011

Good Tools for Debugging Bash Scripts

# builtin for displaying a list of all settable options
shopt

# to see if your process is in a login shell
shopt | grep login

# Print commands and their arguments as they are executed
set -x
# turns off printing of commands
set +x

# standard way to determine linux distribution
lsb_release -a

# current process id
$$

# display cmdline arguments to your script
cat /proc/$$/cmdline

# Not a tool, but something good to know
# $@ vs $*
## set ARGV
set a b c
## dollar star will expand to a single string
for x in "$*"; do echo $x; done
OUTPUT> a b c
## dollar at sign will expand to array of separate elements.
for x in "$@"; do echo $x; done
OUTPUT>
a
b
c

Friday, May 20, 2011

bash type vs unix which

The real difference between type and which.

Avoid which, the exit code isn't guaranteed to exist and some operating systems change the output.

Thursday, May 19, 2011

echo Bash{Brace Expansion, Buildin Type}

Today a colleague of mine, Elliot Shank (@clonezone on twitter), introduced me to two new bash commands that I think are pretty useful. I've decided to blog about them in the hopes that the commands will stick.

First Brace Expansion. You can read a nice over view of Brace Expansion here. The summary gives a nice example of using it to make mulitple directories. You can also use it to rename a file that is in a deep directory structure, for example like this:

mv /site/web/hello_world/version1.0.0/data/2010/05/17/foo{,.bar}

So rather than typing out the whole directory structure again you just have to type it once! The above expands to:

mv /site/web/hello_world/version1.0.0/data/2010/05/17/foo /site/web/hello_world/version1.0.0/data/2010/05/17/foo.bar

Don't forget the ',' in the braces either. Basically the braces are being expanded and the first element is nothing.

The second command is the bash builtin command type. Type doesn't have a man page but you can get information about it by using the built in help and by typing type:

help type
type type

type is an alternative to which. The big difference is that which is an external command whereas type is a bash builtin. You can read more about it here. With type(and which) you can things like vim `type -p foo` which is kind of nice.

In my time tests these were my results:
using type:
real 0m0.000s
user 0m0.000s
sys 0m0.000s

Using which
real 0m0.003s
user 0m0.001s
sys 0m0.002s

So type is a little faster.

Tuesday, May 17, 2011

Git – setting local repo and pushing to new remote

Initialize local git repository
git init

Add and commit all the files to the local repo
git add *
git commit -m 'nick - git init'

Create your remote repository


Locally add your remote repository
git remote add origin

Locally setup tracking so we can do 'git pull' rather than 'git pull origin master'
git branch --set-upstream master origin/master