# Adding methods to native ruby class within rails

**URL:** <https://www.ruby-forum.com/t/adding-methods-to-native-ruby-class-within-rails/142878>\
**Category:** Rails\
**Created:** [July 12, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/adding-methods-to-native-ruby-class-within-rails/142878 "2008-07-12T15:56:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [July 12, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/adding-methods-to-native-ruby-class-within-rails/142878/1 "2008-07-12T15:56:41Z")

</div>

hi !

i’d like to add my own method to the ruby string class, do you know how  
to do this properly in the rails framework?

itkin

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [July 12, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/adding-methods-to-native-ruby-class-within-rails/142878/2 "2008-07-12T16:07:29Z")

</div>

Michael G. wrote:

> Something like this?
> 
> > > “foo”.blarg  
> > > NoMethodError: undefined method `blarg’ for “foo”:String  
> > > from (irb):6  
> > > from :0
> > 
> > > class String  
> > > def blarg  
> > > “blarg”  
> > > end  
> > > end  
> > > =\> nil
> > 
> > > “foo”.blarg  
> > > =\> “blarg”

yes excactly, but where to place this code in the framework ?

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [July 12, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/adding-methods-to-native-ruby-class-within-rails/142878/3 "2008-07-12T16:01:27Z")

</div>

Something like this?

> > “foo”.blarg  
> > NoMethodError: undefined method `blarg’ for “foo”:String  
> > from (irb):6  
> > from :0
> 
> > class String  
> > def blarg  
> > “blarg”  
> > end  
> > end  
> > =\> nil
> 
> > “foo”.blarg  
> > =\> “blarg”

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [July 12, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/adding-methods-to-native-ruby-class-within-rails/142878/4 "2008-07-12T17:19:57Z")

</div>

Michael G. wrote:

> Ahh, I understand your question.
> 
> I have a private plugin I made for this sort of thing. Just look up  
> how to make a very simple plug-in, and you’ll be set. If you do that,  
> you can also maintain it and use it easily in other projects.
> 
> –Michael

thanks works great, here is a nice tuto to create its own plugins :  
[http://railsforum.com/viewtopic.php?id=682](http://railsforum.com/viewtopic.php?id=682)

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [July 12, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/adding-methods-to-native-ruby-class-within-rails/142878/5 "2008-07-12T16:10:15Z")

</div>

Ahh, I understand your question.

I have a private plugin I made for this sort of thing. Just look up  
how to make a very simple plug-in, and you’ll be set. If you do that,  
you can also maintain it and use it easily in other projects.

–Michael

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [July 16, 2008, 7:00am UTC](https://www.ruby-forum.com/t/adding-methods-to-native-ruby-class-within-rails/142878/6 "2008-07-16T07:00:45Z")

</div>

I’d place it in an initializer.

Tiago M.

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [July 13, 2008, 2:40am UTC](https://www.ruby-forum.com/t/adding-methods-to-native-ruby-class-within-rails/142878/7 "2008-07-13T02:40:19Z")

</div>

I place the following in MyApp/lib/time\_extensions.rb

class Time  
def beginning\_of\_hour  
self-(self.min).minutes-self.sec  
end  
def end\_of\_hour  
self.beginning\_of\_hour + 1.hour - 1.second  
end  
def beginning\_of\_minute  
self-self.sec  
end  
def end\_of\_minute  
self.beginning\_of\_minute + 1.minute - 1.second  
end  
def beginning\_of(secs=5.minutes)  
return self if secs \>= 1.day/2  
secs\_today = self - self.beginning\_of\_day  
periods\_today = (secs\_today/secs).floor  
self.beginning\_of\_day + periods\_today \* secs  
end  
def end\_of(secs=5.minutes)  
return self if secs \>= 1.day/2  
beginning\_of(secs) + secs - 1.second  
end  
end

On Jul 12, 11:19 am, nico Itkin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
