From: David Masover Date: 2009-11-21T04:27:03+09:00 Subject: Re: Something like puts On Friday 20 November 2009 11:38:19 am Ralph Shnelvar wrote: > Newbie question: > > I need/want a function like puts ... that is, a function known > globally. > > I have read the Pickaxe section on mixins and > http://www.ruby-forum.com/topic/68638 and I'm going nuts. > > Specifically I am trying to do > > > module ApplicationHelper > puts "Hearyee ApplicationHelper" > > def at_file_line_msg(file, line, msg) > file + " @ " + line.to_s + ":" + msg > end > > puts at_file_line_msg(__FILE__, __LINE__, "") > end > > The > puts at_file_line_msg(__FILE__, __LINE__, "") > generates the right output. > > When I attempt to mixin ApplicationHelper into a classand then attempt > to call > puts at_file_line_msg(__FILE__, __LINE__, "") > I get a "module not defined" error. You're going to have to provide a lot more context. What specific error are you getting, and where, specifically, are you calling this? For example: > I think this has something to do with Modules not mixing in, uh, class > methods. Frankly, I'm not following the arguments. If you're doing this: class Foo include SomeHelper puts at_file_line_msg(__FILE__,__LINE__,'') end In that case, yes, it has to do with class methods not being defined. You _may_ be able to get around this by doing something like: module ApplicationHelper module ClassMethods def at_file_line_msg ... ... end def self.included mod mod.extend ClassMethods end end I'm not sure, though. It's been awhile since I've dug into Rails. Regardless, Helpers are only available in certain places -- for instance, I don't think they work inside the model. So you may have to put it somewhere else... > So ... what is the right way to create a function like puts that is > known everywhere? Put it in Kernel, I think. But the right way is to not do that unless you have to.