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.


Tuesday, May 24, 2011

Good Tools for Debugging Bash Scripts

# builtin for displaying a list of all settable options

# 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

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

Monday, September 28, 2009

Sweet Home Chicago

Arrived back in Chicago yesterday. After 13 hours of travel, we were exhausted. Gabrielle went to sleep at 7 and I followed at 8 (after watching the Bears beat Seattle!).

Of course everything didn't go completely as planned. We took the Sheraton taxi (which to my surprise was a Mercedes) and got the Prague airport an hour and a half before our flight. Plenty of time right? Only if United properly ticketed you a seat on the Lufthansa plane! UGH, so we spent 15 minutes (at 1.50 a minute) on the phone trying to resolve the problem with United. Finally with 20 minutes to board we got the crucial ticket number Lufthansa needed to get us on the plane. After that everything was pretty smooth.

We arrived in Chicago around 3 and finally got home around 4:30. On the taxi ride home I just rolled down the window and breathed in the sweet Chicago air.

Saturday, September 26, 2009

Papal Tease

Today as been pretty busy so far. This morning Gabrielle and I walked to the Prague Castle-- carefully avoiding the Charles Bridge (which was packed with tourists). On our way we passed the Church of Our Lady beneath the Chain and ran into a sea of people. Hundreds of people were lined up and after first thinking this was the line to the castle we soon discovered that the Pope was in town and would be coming this way!

I immediately started thinking about what it would be like to see the Holy Father coming down a street we had just stumbled on. So we waited 15 minutes and then the bells began ringing and the police started pushing people back-- off in the distance we could hear police sirans. This was it!

And then the motorcycles appeared, next a police car, then a black sedan, another black sedan, another black sedan, a van, another van and then...that was it. Police started walking away and a huge groan sounded through the crowd. I just started laughing. How silly to think that we would be so lucky!