Thursday, July 21, 2011

Ruby learnings

I had a brief foray into Ruby land recently, so thought I would record some lessons learnt here.

Interactive ruby is like the really poor cousin to ipython. Commandline completion is not on by default, you want to enable it using:

alias irb='irb -r irb/completion'
and apparently the only way to get syntax highlighting is to install the wirble gem.

There are a bunch of ways to run shell commands. The one I used was %x:

cmd = "dscl . -read #{file} #{key} 2> /dev/null"
  output = %x{#{cmd}}

I was looking for a equivalent of the python string function 'startswith'. The ruby equivalent actually has a ? in the function name (WTF?):

if output.start_with?("#{key}: ")

No comments: