T
trans. (T. Onoma)
Here's a generic routine I'm working on:
class String
def last=(str, separator=$/)
separator = '' unless separator
raise "separator must be a String" unless String === separator
s = self.split(separator, -1)
s[-1] = str
self.replace(s.join(separator))
end
end
$/ = "\n"
s = "ab\nc"
s.last = "123"
p s
# => "ab\n123"
Now try:
$/ = ""
s = "abc"
s.last = "123"
p s
Unfortunately this does not give a congruent result.
As an aside, this relates to the split -1 since any generic routine will not
know what the split separator is and therefore will require the -1. Though my
analysis is not complete, I continue to find that in most cases the -1
parameter is either required, or at worst, inconsequential.
T.
class String
def last=(str, separator=$/)
separator = '' unless separator
raise "separator must be a String" unless String === separator
s = self.split(separator, -1)
s[-1] = str
self.replace(s.join(separator))
end
end
$/ = "\n"
s = "ab\nc"
s.last = "123"
p s
# => "ab\n123"
Now try:
$/ = ""
s = "abc"
s.last = "123"
p s
Unfortunately this does not give a congruent result.
As an aside, this relates to the split -1 since any generic routine will not
know what the split separator is and therefore will require the -1. Though my
analysis is not complete, I continue to find that in most cases the -1
parameter is either required, or at worst, inconsequential.
T.