Or to be more exact, how would I do the following in ruby?

while ($string =~ /\0/g) {
    $new_string .= sprintf '\%o', (pos $string) - 1;
}