# Creating a custom function that can be called from an instance variable

**URL:** <https://www.ruby-forum.com/t/creating-a-custom-function-that-can-be-called-from-an-instance-variable/144460>\
**Category:** Rails\
**Created:** [July 29, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/creating-a-custom-function-that-can-be-called-from-an-instance-variable/144460 "2008-07-29T21:07:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [July 29, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/creating-a-custom-function-that-can-be-called-from-an-instance-variable/144460/1 "2008-07-29T21:07:36Z")

</div>

Hello all,

I have a whole bunch of time stamps which I need to display in  
fractional hours or fractional days as the case may be.

## Is there any chance I could do something like this: \<\> def HourDisp … end @time = 12:30:00

## \<\> \<%= @time.HourDisp %\>

Shows:  
12.5

Thank You very much@

---

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [July 29, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/creating-a-custom-function-that-can-be-called-from-an-instance-variable/144460/2 "2008-07-29T21:25:46Z")

</div>

On Jul 29, 2008, at 12:06 PM, Nik wrote:

> ## end @time = 12:30:00
> 
> ## \<\> \<%= @time.HourDisp %\>
> 
> Shows:  
> 12.5

Nik -

Create a helper method named say “time\_as\_fraction” and pass your time  
into it. Make that method do whatever munging you need it to do.

-philip

---

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [August 1, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/creating-a-custom-function-that-can-be-called-from-an-instance-variable/144460/3 "2008-08-01T15:22:35Z")

</div>

So:  
#t in hh:mm:ss format  
def HourDisp(t)  
#t gets split in three parts hh,mm, and ss  
new\_t = t.split(’:’)  
h=new\_t[0] + new\_t[1]/60 + new\_t[2]/3600  
return h  
end

\<\>  
\<%= HourDisp(12:30:00) %\>  
#12.5

Thank You So Much Philip!

---

<div class="post-metadata">

**Author:** ![nik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6de8d8/32.png) [@nik](https://www.ruby-forum.com/u/nik)\
**Post date:** [August 1, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/creating-a-custom-function-that-can-be-called-from-an-instance-variable/144460/4 "2008-08-01T15:24:56Z")

</div>

I tried it and I got it to work, Philip!

Thank You so much for your time. It’s probably not my place to say  
that this stuff is probably elementary. This is why: Thank you so much  
for your time!!

Cheers!
