Forgive me if this is a really stupid question, I''ve only started learning Rails. After some Googling and talking to some people, I still don''t know how to do this. Basically, what I want to do is to be able to create a class that I can use anywhere in my application, kind of like the way you can use a method anywhere when you define it in the ApplicationHelper module. Specifically, I don''t like using methods like truncate(string, length) and h(string), and I''d much rather use them in the form of string.truncate(length) and string.h(), Could anybody tell me how to do this? Thanks in advance. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
one way is to define them on the String class: class String def h ...do stuff... end end save this in a file somewhere, e.g. lib/my_string_methods.rb and include it in your environment.rb require File.dirname(__FILE__) + "/../lib/my_string_methods" On Aug 21, 1:14 pm, Shane <the...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Forgive me if this is a really stupid question, I''ve only started > learning Rails. > > After some Googling and talking to some people, I still don''t know how > to do this. > > Basically, what I want to do is to be able to create a class that I > can use anywhere in my application, kind of like the way you can use a > method anywhere when you define it in the ApplicationHelper module. > > Specifically, I don''t like using methods like truncate(string, length) > and h(string), and I''d much rather use them in the form of > string.truncate(length) and string.h(), > > Could anybody tell me how to do this? Thanks in advance.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
also keep in mind that rails defines some String helpers already. e.g. to truncate a string, you could do ''this is my string''.first(5) # = ''this '' or use core ruby methods: ''this is my string''[0,5] # = ''this '' On Aug 21, 2:55 pm, jemminger <jemmin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> one way is to define them on the String class: > > class String > def h > ...do stuff... > end > end > > save this in a file somewhere, e.g. lib/my_string_methods.rb > > and include it in your environment.rb > require File.dirname(__FILE__) + "/../lib/my_string_methods" > > On Aug 21, 1:14 pm, Shane <the...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Forgive me if this is a really stupid question, I''ve only started > > learning Rails. > > > After some Googling and talking to some people, I still don''t know how > > to do this. > > > Basically, what I want to do is to be able to create a class that I > > can use anywhere in my application, kind of like the way you can use a > > method anywhere when you define it in the ApplicationHelper module. > > > Specifically, I don''t like using methods like truncate(string, length) > > and h(string), and I''d much rather use them in the form of > > string.truncate(length) and string.h(), > > > Could anybody tell me how to do this? Thanks in advance.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You should be able to created your class under the lib directory. On Aug 21, 2007, at 1:14 PM, Shane wrote:> > Forgive me if this is a really stupid question, I''ve only started > learning Rails. > > After some Googling and talking to some people, I still don''t know how > to do this. > > Basically, what I want to do is to be able to create a class that I > can use anywhere in my application, kind of like the way you can use a > method anywhere when you define it in the ApplicationHelper module. > > Specifically, I don''t like using methods like truncate(string, length) > and h(string), and I''d much rather use them in the form of > string.truncate(length) and string.h(), > > Could anybody tell me how to do this? Thanks in advance. > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks a lot! This does exactly what I was looking for. It''s annoying that I have restart script/server everytime I make a change, but it''ll do. Thanks! On Aug 21, 7:55 pm, jemminger <jemmin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> one way is to define them on the String class: > > class String > def h > ...do stuff... > end > end > > save this in a file somewhere, e.g. lib/my_string_methods.rb > > and include it in your environment.rb > require File.dirname(__FILE__) + "/../lib/my_string_methods" > > On Aug 21, 1:14 pm, Shane <the...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Forgive me if this is a really stupid question, I''ve only started > > learning Rails. > > > After some Googling and talking to some people, I still don''t know how > > to do this. > > > Basically, what I want to do is to be able to create a class that I > > can use anywhere in my application, kind of like the way you can use a > > method anywhere when you define it in the ApplicationHelper module. > > > Specifically, I don''t like using methods like truncate(string, length) > > and h(string), and I''d much rather use them in the form of > > string.truncate(length) and string.h(), > > > Could anybody tell me how to do this? Thanks in advance.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Basically, what I want to do is to be able to create a class that I > can use anywhere in my application, kind of like the way you can use a > method anywhere when you define it in the ApplicationHelper module.as of that:>> You should be able to created your class under the lib directory.> Specifically, I don''t like using methods like truncate(string, length) > and h(string), and I''d much rather use them in the form of > string.truncate(length) and string.h(), > > Could anybody tell me how to do this? Thanks in advance.if you want to define a method for an object (instance method), you''ll need to define that in that instance''s class; so, if you want to play around with strings, you''ll need to modify that class. class String def truncate(size) self[0..size] # self is the instance being worked on end end so now, "apples".truncate(3) # returns "app" some more examples: http://www.donttrustthisguy.com/2005/12/31/ruby-extending-classes-and-method-chaining/ http://rails.aizatto.com/2007/06/01/ruby-and-open-classes/ (scroll down, it''s an article) hope it helps out :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shane wrote:> Thanks a lot! This does exactly what I was looking for. > > It''s annoying that I have restart script/server everytime I make a > change, but it''ll do. Thanks!If you want to do modification without restarting the server instead of using - require File.dirname(__FILE__) + "/../lib/my_string_methods" put it inside Rails::Initializer in your environment.rb using the config.load_path directive. config.load_paths += %W( #{RAILS_ROOT}/lib/<you class folder>) cheers....... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---