Hello, I would like to add functionality to the String class....where do I do this? Do I create a new model (if so, what do I call it?) and put something like: class String def my_new_string_method blah blah end end Thanks for any pointers. -- Posted via http://www.ruby-forum.com/.
Josh Susser
2006-Aug-11 01:40 UTC
[Rails] Re: Newbie Question -- Override String functionality
cman wrote:> Hello, > > I would like to add functionality to the String class....where do I do > this? > > Do I create a new model (if so, what do I call it?) and put something like: > > class String > def my_new_string_method > blah blah > end > endYou don''t need to create a new model. You can just paste that code to extend String into your config/environments.rb and you''ll be good to go. That''s a bit messy way to organize monkey patches, but it works. If you get to the point where that becomes a pain, move the monkey patches into files in the lib directory and require them from environment.rb. -- Josh Susser http://blog.hasmanythrough.com/ -- Posted via http://www.ruby-forum.com/.
>> class String >> def my_new_string_method >> blah blah >> end >> end > > You don''t need to create a new model. You can just paste that code to > extend String into your config/environments.rb and you''ll be good to go. > That''s a bit messy way to organize monkey patches, but it works. If you > get to the point where that becomes a pain, move the monkey patches into > files in the lib directory and require them from environment.rb.Another option is to bundle your extension methods into a module, and include it into the String class. Say: module MyStringModule def my_foo "foo.#{to_s}" end end String.send(:include, MyStringModule) Note the use of send, as the include method is private. The nifty part of this implementation is that you get some information in the class about which modules it uses. Type: String.ancestors and the list will include "MyStringModule" -- Posted via http://www.ruby-forum.com/.
Mat Schaffer
2006-Aug-11 12:31 UTC
[Rails] Newbie Question -- Override String functionality
On Aug 10, 2006, at 8:16 PM, cman wrote:> Hello, > > I would like to add functionality to the String class....where do I do > this? > > Do I create a new model (if so, what do I call it?) and put something > like: > > class String > > def my_new_string_method > blah blah > end > > > end > > Thanks for any pointers.I usually put this sort of stuff in /lib and then require it from config/environment.rb -Mat
Josh Susser wrote:> cman wrote: >> Hello, >> >> I would like to add functionality to the String class....where do I do >> this? >> >> Do I create a new model (if so, what do I call it?) and put something like: >> >> class String >> def my_new_string_method >> blah blah >> end >> end > > You don''t need to create a new model. You can just paste that code to > extend String into your config/environments.rb and you''ll be good to go. > That''s a bit messy way to organize monkey patches, but it works. If you > get to the point where that becomes a pain, move the monkey patches into > files in the lib directory and require them from environment.rb. > > -- > Josh Susser > http://blog.hasmanythrough.com/I tried adding it to configs/environments.rb, and i get the following error when booting WebBrick: Invalid char ''\223'' in expression, Invalid char ''\224'' in expression. Here is the code: class String def capitalize_each self.split(? ?).each{|word| word.capitalize!}.join(? ?) end def capitalize_each! replace capitalize_each end end -- Posted via http://www.ruby-forum.com/.
Curtis Summers
2006-Aug-11 21:43 UTC
[Rails] Re: Newbie Question -- Override String functionality
> I tried adding it to configs/environments.rb, and i get the following > error when booting WebBrick: Invalid char ''\223'' in expression, Invalid > char ''\224'' in expression.> Here is the code: > class String > def capitalize_each > self.split(? ?).each{|word| word.capitalize!}.join(? ?) > end > def capitalize_each! > replace capitalize_each > end > endNot for certain on this, a likely cause is fancy quotes. If you copy and pasted this code from somewhere (like a blog), the double-quotes may be fancy quotes. Just try retyping the quotes in your text editor. -- Posted via http://www.ruby-forum.com/.
Hasan Diwan
2006-Aug-14 07:56 UTC
[Rails] Re: Newbie Question -- Override String functionality
>From an organisational perspective, I would think this would belong inapp/helper/application_helper.rb or perhaps an engine. -- Cheers, Hasan Diwan <hasan.diwan@gmail.com> -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060814/ec62172d/attachment.html