# Creating a ! method

**URL:** <https://rubytalk.org/t/creating-a-method/18956>\
**Category:** ruby-talk\
**Created:** [13 June 2005 22:14 UTC](https://rubytalk.org/t/creating-a-method/18956 "2005-06-13T22:14:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gavin\_Kistner1](https://avatars.discourse-cdn.com/v4/letter/g/e19b73/32.png) [@Gavin\_Kistner1](https://rubytalk.org/u/Gavin_Kistner1)\
**Post date:** [13 June 2005 22:14 UTC](https://rubytalk.org/t/creating-a-method/18956/1 "2005-06-13T22:14:42Z")

</div>

One potential way to accomplish this is what I did in my MutableTime class[1]. Create a new class that has an internal Time instance variable and -- when you want to 'round' your time -- replace this instance variable with the new Time instance. Judicious use of #method\_missing allows your class to pass off any methods it doesn't understand to the time instance.

[1] [File: MutableTime.rb](http://phrogz.net/RubyLibs/rdoc/files/MutableTime_rb.html)

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
> 
> From: Jamis Buck [mailto:jamis@37signals.com]  
> Sent: Mon 6/13/2005 11:55 AM  
> To: ruby-talk ML  
> Subject: Re: creating a ! method
> 
> On Jun 13, 2005, at 11:51 AM, Kurt V. Hindenburg wrote:
> 
> > Hello,  
> > &nbsp;&nbsp;I have this code. It works fine but I would like to use  
> > round\_to\_interval!.  
> > I could not figure out how to code that. Any suggestions?
> > 
> > class Time  
> > &nbsp;&nbsp;&nbsp;def round\_to\_interval!  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# how?  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;def round\_to\_interval  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self - (self.min % 10) \* 60  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> 
> Time objects are immutable (like numbers, symbols, etc.), so you  
> cannot alter them directly.
> 
> - Jamis
