eval question ?

A

Ak 756

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.
 
N

Nobuyoshi Nakada

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"
 
M

Michael Fellinger

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.

http://p.ramaze.net/191

^ manveru
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top