Eval question?

Hi

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”

puts modifytime(time,add5second) => “00:11:25”
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.

Hi,

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?

“+5”.to_i == 5
“-50”.to_i == -50

(Time.parse(“00:11:20”)+"+5".to_i).strftime("%T") # => “00:11:25”

On Jan 15, 2008 11:56 AM, Ak 756 [email protected] wrote:

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.

http://p.ramaze.net/191

^ manveru

Michael F. wrote:

On Jan 15, 2008 11:56 AM, Ak 756 [email protected] wrote:

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.

http://p.ramaze.net/191

^ manveru

Cool website, cool solution. Thanks very much :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs