Category Archives: Programming

Picking out a part of a string

A limited use-case, but in case you get to the situation where you need the following bold part of a string:


(The asterisk matching any word)

And here’s the slightly messy solution I used; in this case, I needed the first three directories of the path the script was running in:

pubpath=`echo $fullpath | rev | cut -c13- | rev`

So I’ve reversed it so I know the exact number of characters from the start of the string that I need, used cut to select part of the string; from character 13 to the end (reversed form of /home/*/public_html), then reserved it back again.

Ruby methods after maths

Not quite sure what to call this post…But in any case:“%Y-%m-%d”)

This results in an error; the strftime tries to work on the ‘7’ only, for some reason.

You can fix it by expanding the maths into it’s full form (ie, a method):“%Y-%m-%d”)

Learning Ruby notes

I’ve just finished my first plugin for Redmine, learning Ruby along the way.  It’s a bit messy, but it’s simple and does it’s job.  Some things that I learnt along the way:

When you’re in a view, <% tag is used for codes, and <%= is used to print the result of the codes to the screen.  For example:
<% issues = issues.sort_by {|issue| issue[‘date_relative’]} %> – sorts the array
<%= issues = issues.sort_by {|issue| issue[‘date_relative’]} %> – sorts the array and prints the output

debug and inspect are used for debugging; like so:
debug issues
debug is a bit more useful, I found

Use .class to figure out the class the variable belongs to; in Ruby, arrays and hashes are classes as well, so it can be quite useful to find out what kind of data you’re looking at.

The output of debug can be confusing, for a long time I was trying to figure out why I couldn’t access some of the attributes of an array I was looking at.  I realised why after doing .count; there was one value in the array, and that array was an array containing the data I was trying to find (this is difficult to figure out from debug if you are using it for the first time).  So:
issue.value returned an error
issue[0].value was ok

It seems lots of RoR applications use ActiveRecords; this page is useful for further methods for debugging/accessing data:
(In my case, Redmine classes are subclasses of ActiveRecord::Base)

ffmpeg killing your loops?

The ‘press q to stop blahblah’ steals the STDIN, so it stops listening to the STDIN from the loop and so only processes the first result.


ffmpeg etcetc lalala blahblah < /dev/null



Iterate through find results in bash

find [insert parameters here] | while read line
do [insert stuff to do here]

You can use the variable $line here, for the path to each file that ‘find’ finds.  And you can use `(basename $line)` to get the filename only (removing the path).  For some reason, I couldn’t do, eg NAME=`(basename $line)` or NAME=$(basename $line).  (Well, I could, it worked, however at the end of the script it gave me basename missing operand error.  If anyone could enlighten me…