SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Where to over-ride inbuilt functions

    From the Gotcha thread.
    Quote Originally Posted by stereofrog
    The good about ruby is that you can change almost everything:
    Code:
    class Fixnum
    	alias idiv /
    	def /(other)
    		self.to_f / other
    	end
    end
    
    print 1/2
    I have a problem in that I am never quite sure where I should put code like that. Should I create a new file called fixnum.rb and put it in my application's lib folder, or is there a better way to implement a cross application over-ride of an inbuilt class method?

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're using Rails? I think this belongs in the lib folder. You can replace the lib folder with a link to a central lib folder for all your applications.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ReggieB
    From the Gotcha thread.

    I have a problem in that I am never quite sure where I should put code like that. Should I create a new file called fixnum.rb and put it in my application's lib folder, or is there a better way to implement a cross application over-ride of an inbuilt class method?
    Any changes to the core classes should be in different files from your main application. This allows you to use these modifications independently from the application, if you should ever decide to.

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sgarissta
    Any changes to the core classes should be in different files from your main application. This allows you to use these modifications independently from the application, if you should ever decide to.
    Thanks for that. However, altering the way Ruby behaves on a computer for all applications seems to me to be relatively easy. I am really after a how I should do it if I only want to change the behaviour for a particular application. For that, I guess from Fenrir2's response, that lib is the right place to put the code.

    Would the filename need to have the class name, or is the name irrelevant?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •