# Eval question?

**URL:** <https://www.ruby-forum.com/t/eval-question/126058>\
**Category:** Ruby\
**Created:** [January 15, 2008, 3:55am UTC](https://www.ruby-forum.com/t/eval-question/126058 "2008-01-15T03:55:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ak756](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5a626/32.png) [@ak756](https://www.ruby-forum.com/u/ak756)\
**Post date:** [January 15, 2008, 3:55am UTC](https://www.ruby-forum.com/t/eval-question/126058/1 "2008-01-15T03:55:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ak756](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5a626/32.png) [@ak756](https://www.ruby-forum.com/u/ak756)\
**Post date:** [January 15, 2008, 4:08am UTC](https://www.ruby-forum.com/t/eval-question/126058/2 "2008-01-15T04:08:41Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![ak756](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5a626/32.png) [@ak756](https://www.ruby-forum.com/u/ak756)\
**Post date:** [January 15, 2008, 4:12am UTC](https://www.ruby-forum.com/t/eval-question/126058/3 "2008-01-15T04:12:12Z")

</div>

On Jan 15, 2008 11:56 AM, Ak 756 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://p.ramaze.net/191)

^ manveru

---

<div class="post-metadata">

**Author:** ![ak756](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5a626/32.png) [@ak756](https://www.ruby-forum.com/u/ak756)\
**Post date:** [January 15, 2008, 5:01am UTC](https://www.ruby-forum.com/t/eval-question/126058/4 "2008-01-15T05:01:48Z")

</div>

Michael F. wrote:

> On Jan 15, 2008 11:56 AM, Ak 756 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://p.ramaze.net/191)
> 
> ^ manveru

Cool website, cool solution. Thanks very much 🙂
