From: Siep Korteling Date: 2008-05-07T18:50:49+09:00 Subject: Re: Where to put code for extending a class? Damjan Rems wrote: > Zoop Zoop wrote: >> Phillip Gawlowski wrote: >>> A: It makes it difficult to follow the conversation >>> !: Why is top posting bad? >> >> Hehe, this took me a second to understand, but I got your clue. >> Is in-between-posting ok? >> >>> | Do you mean adding the code directly to the original string.rb file? >>> >>> No. Any source file in your application / directory structure. as long >>> as require can find it, you can use it. >> >> Ok, but where exactly is the recommended place to put code for >> extensions like the one I want to do? > > Anywhere in your program flow. > (...) > TheR Well, anywhere before you actually use the new method. If you would move the class String...end block to the end of the code, the following would not work: class String def capitalize_each self.split(" ").each{|word| word.capitalize!}.join(" ") end def capitalize_each! replace capitalize_each end end print "Type some words here: " str = gets print "This is the capitalize method: " puts str.capitalize print "and this is the custom made capitalize_each method: " puts str.capitalize_each print "The original string: " puts str str.capitalize_each! print "Now it's changed: " puts str puts "(any key to exit)" a = gets regards, Siep -- Posted via http://www.ruby-forum.com/.