C
Claus Guttesen
Hi.
I need to get the last three characters in a string. I couldn't find a
function in the String class so I came up with this:
class Remainder
attr_writer :n
def initialize(t = "", n = 0)
@t = t.to_s
@n = n.to_i
end
def get
get_remaining_letters
end
private
def get_remaining_letters
s = @t.size
if s < @n
r = 0
else
r = s - @n
end
ft = @t.slice(r, @n)
end
end
t = Remainder.new("Guttesen", 5)
puts t.get
tesen
t.n = 4
puts t.get
esen
Did I reinvent the wheel or does it make sense to add it to String?
regards
Claus
I need to get the last three characters in a string. I couldn't find a
function in the String class so I came up with this:
class Remainder
attr_writer :n
def initialize(t = "", n = 0)
@t = t.to_s
@n = n.to_i
end
def get
get_remaining_letters
end
private
def get_remaining_letters
s = @t.size
if s < @n
r = 0
else
r = s - @n
end
ft = @t.slice(r, @n)
end
end
t = Remainder.new("Guttesen", 5)
puts t.get
tesen
t.n = 4
puts t.get
esen
Did I reinvent the wheel or does it make sense to add it to String?
regards
Claus