You could use split and join methods from String and Array objects:
>> s = "/var/usr/local/noob/"
=> "/var/usr/local/noob/"
>> p = s.split('/')
=> ["", "var", "usr", "local", "noob"]
>> p.slice!(-1)
=> "noob"
>> p.join('/')
=> "/var/usr/local"
>> p.slice!(-1)
=> "local"
>> p.join('/')
=> "/var/usr"
>> p.slice!(-1)
=> "usr"
>> p.join('/')
=> "/var"
Now you just have to put these two functions in an object that holds the state of the string whose last block is being removed.
I don't know much of ruby but something like the following could be implemented:
// Pseusdo - Java
class LastBlockRemoved {
private String string;
private Array currentState;
public String remove_last_block_of_string() {
currentState = string.split("/");
string = currentState.join("/");
return string
}
}
Or however that is coded in Ruby :P
BTW It would be great if someone could actually implement that class, so I can learn how it is done ;)