I hava a string indicate a hh/mm/ss time and some other strings indicat
operations on the string. For example , string “+5” means add 5 second.
Here is my code:
def modifytime(s1,s2)
if s2[0,1] == “+”
(Time.parse(s1)+s2[1,s2.length-1].to_i).to_s.split[3]
else if s2[0,1] == “-”
(Time.parse(s1)-s2[1,s2.length-1].to_i).to_s.split[3]
end
end
end
time = “00:11:20”
add5second = “+5”
sub50second = “-50”
The result is what I expected but I think the modifytime is ugly.
Can I use ‘eval’ so that I don’t need to judge “+” or “-”?
Or, any elegant solutions?
Thanks in advance.
At Tue, 15 Jan 2008 11:56:04 +0900,
Ak 756 wrote in [ruby-talk:287454]:
The result is what I expected but I think the modifytime is ugly.
Can I use ‘eval’ so that I don’t need to judge “+” or “-”?
Or, any elegant solutions?
else if s2[0,1] == “-”
puts modifytime(time,sub50second) => “00:10:30”
The result is what I expected but I think the modifytime is ugly.
Can I use ‘eval’ so that I don’t need to judge “+” or “-”?
Or, any elegant solutions?
Thanks in advance.
else if s2[0,1] == “-”
puts modifytime(time,sub50second) => “00:10:30”
The result is what I expected but I think the modifytime is ugly.
Can I use ‘eval’ so that I don’t need to judge “+” or “-”?
Or, any elegant solutions?
Thanks in advance.