# Another (simple!) eval question!

**URL:** <https://www.ruby-forum.com/t/another-simple-eval-question/117997>\
**Category:** Ruby\
**Created:** [October 23, 2007, 10:34am UTC](https://www.ruby-forum.com/t/another-simple-eval-question/117997 "2007-10-23T10:34:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mouse\_059](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@mouse\_059](https://www.ruby-forum.com/u/mouse_059)\
**Post date:** [October 23, 2007, 10:34am UTC](https://www.ruby-forum.com/t/another-simple-eval-question/117997/1 "2007-10-23T10:34:06Z")

</div>

greetings Rubyists,

Still trying to wrap my head around metaprogramming.

def analyze(args)

# stuff

# total is defined up here and is equal to some amount of seconds

elapsed (say 100,000)

day, hour, min = 0, 0, 0  
loop do day += 1; total -= 86400; break if total \< 0; end; day -= 1;  
total += 86400  
loop do hour += 1; total -= 3600; break if total \< 0; end; hour -=  
1; total += 3600  
loop do min += 1; total -= 60; break if total \< 0; end; min -= 1;  
total += 60

puts “your total is #{day} days, #{hour} hours, #{min} mins,  
#{total} secs”  
end

as you can see, i’d like to refactor that so i just have to  
dotime( :day, 86400 )  
dotime( :hour, 3600 )  
dotime( :min, 60 ) (i can hardcode ‘total’ in the  
function, doesn’t matter?)

But no eval I use works, either the eval isn’t defined for my class  
(assume main:Object) or I get "no method for + (nilpointer). I read  
about how you shouldn’t use string eval, etc. A simple pointer to  
some help would be awesome! Thanks guys.

PS, if a time function does this, don’t spoil the metaprogramming for  
me? I’d like to learn anyway. 🙂

-Mike

---

<div class="post-metadata">

**Author:** ![mouse\_059](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@mouse\_059](https://www.ruby-forum.com/u/mouse_059)\
**Post date:** [October 23, 2007, 11:07am UTC](https://www.ruby-forum.com/t/another-simple-eval-question/117997/2 "2007-10-23T11:07:01Z")

</div>

2007/10/23, mouse\_059 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> loop do day += 1; total -= 86400; break if total \< 0; end; day -= 1;  
> as you can see, i’d like to refactor that so i just have to  
> PS, if a time function does this, don’t spoil the metaprogramming for  
> me? I’d like to learn anyway. 🙂

I have no idea why you think there should be an eval in there.

What I’d do: I’d put sizes into an array and use that together with  
modulus calculations to split up seconds into years, months, days etc.

Kind regards

robert
