[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

eval question ?

Ak 756

1/15/2008 2:56:00 AM

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.
--
Posted via http://www.ruby-....

3 Answers

Nobuyoshi Nakada

1/15/2008 3:08:00 AM

0

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"

--
Nobu Nakada

Michael Fellinger

1/15/2008 3:11:00 AM

0

On Jan 15, 2008 11:56 AM, Ak 756 <macro.peng@gmail.com> wrote:
> 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.rama...

^ manveru

Ak 756

1/15/2008 4:02:00 AM

0

Michael Fellinger wrote:
> On Jan 15, 2008 11:56 AM, Ak 756 <macro.peng@gmail.com> 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.rama...
>
> ^ manveru

Cool website, cool solution. Thanks very much :-)
--
Posted via http://www.ruby-....