# Something like puts

**URL:** <https://www.ruby-forum.com/t/something-like-puts/179268>\
**Category:** Ruby\
**Created:** [November 20, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/something-like-puts/179268 "2009-11-20T18:44:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 20, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/something-like-puts/179268/1 "2009-11-20T18:44:23Z")

</div>

Newbie question:

I need/want a function like puts … that is, a function known  
globally.

I have read the Pickaxe section on mixins and  
[Why the lack of mixing-in support for Class methods? - Ruby - Ruby-Forum](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.

I think this has something to do with Modules not mixing in, uh, class  
methods. Frankly, I’m not following the arguments.

So … what is the right way to create a function like puts that is  
known everywhere?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 20, 2009, 6:50pm UTC](https://www.ruby-forum.com/t/something-like-puts/179268/2 "2009-11-20T18:50:40Z")

</div>

Ralph S. wrote:

> Newbie question:
> 
> I need/want a function like puts … that is, a function known  
> globally.  
> […]  
> So … what is the right way to create a function like puts that is  
> known everywhere?

Put it in Object.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 20, 2009, 7:08pm UTC](https://www.ruby-forum.com/t/something-like-puts/179268/3 "2009-11-20T19:08:58Z")

</div>

> I think this has something to do with Modules not mixing in, uh, class  
> methods. Frankly, I’m not following the arguments.
> 
> So … what is the right way to create a function like puts that is  
> known everywhere?

Maybe I’m missing something, but I think what you want is to just  
define it in the default global scope:

irb(main):002:0\> def foo #our global function  
irb(main):003:1\> ‘bar’  
irb(main):004:1\> end  
=\> nil  
irb(main):011:0\> class Blah  
irb(main):012:1\> def b  
irb(main):013:2\> foo #Our function defined earlier  
irb(main):014:2\> end  
irb(main):015:1\> end  
=\> nil  
irb(main):016:0\> b=Blah.new  
=\> #Blah:0x2d58d20  
irb(main):017:0\> b.b  
=\> “bar”  
irb(main):018:0\>

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 20, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/something-like-puts/179268/4 "2009-11-20T20:27:24Z")

</div>

On Friday 20 November 2009 11:38:19 am Ralph S. wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 20, 2009, 10:21pm UTC](https://www.ruby-forum.com/t/something-like-puts/179268/5 "2009-11-20T22:21:35Z")

</div>

Walton H. wrote:

> > I think this has something to do with Modules not mixing in, uh, class  
> > methods. Frankly, I’m not following the arguments.
> > 
> > So … what is the right way to create a function like puts that is  
> > known everywhere?
> 
> Maybe I’m missing something, but I think what you want is to just  
> define it in the default global scope:  
> […]

Right. Which actually puts it in Object. I had forgotten about the  
syntactic sugar.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 20, 2009, 11:46pm UTC](https://www.ruby-forum.com/t/something-like-puts/179268/6 "2009-11-20T23:46:23Z")

</div>

> > > So … what is the right way to create a function like puts that is  
> > > known everywhere?
> > 
> > Maybe I’m missing something, but I think what you want is to just  
> > define it in the default global scope:  
> > […]
> 
> Right. Which actually puts it in Object. I had forgotten about the  
> syntactic sugar.

Does it? I’ve heard that before, but this seems to indicate otherwise.

irb(main):001:0\> def foobar()  
irb(main):002:1\> “Foo”  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> Object.methods.sort  
=\> [:!, :!=, :!~, :\<, :\<=, :\<=\>, :==, :===, :=~, :\>, :\>=, : **id** ,  
: **send** , :a  
llocate, :ancestors, :autoload, :autoload?, :class, :class\_eval,  
:class\_exec, :c  
lass\_variable\_defined?, :class\_variable\_get, :class\_variable\_set,  
:class\_variabl  
es, :clone, :const\_defined?, :const\_get, :const\_missing, :const\_set,  
:constants,  
:define\_singleton\_method, :display, :dup, :enum\_for, :eql?, :equal?,  
:extend, :  
freeze, :frozen?, :gem, :hash, :include?, :included\_modules, :inspect,  
:instance  
\_eval, :instance\_exec, :instance\_method, :instance\_methods,  
:instance\_of?, :inst  
ance\_variable\_defined?, :instance\_variable\_get, :instance\_variable\_set,  
:instanc  
e\_variables, :is\_a?, :kind\_of?, :method, :method\_defined?, :methods,  
:module\_eva  
l, :module\_exec, :name, :new, :nil?, :object\_id, :private\_class\_method,  
:private  
\_instance\_methods, :private\_method\_defined?, :private\_methods,  
:protected\_instan  
ce\_methods, :protected\_method\_defined?, :protected\_methods,  
:public\_class\_method  
, :public\_instance\_method, :public\_instance\_methods, :public\_method,  
:public\_met  
hod\_defined?, :public\_methods, :public\_send, :remove\_class\_variable,  
:respond\_to  
?, :send, :singleton\_methods, :superclass, :taint, :tainted?, :tap,  
:to\_enum, :t  
o\_s, :trust, :untaint, :untrust, :untrusted?]  
irb(main):005:0\> o = Object.new  
=\> #Object:0x2add480  
irb(main):006:0\> o.methods.sort  
=\> [:!, :!=, :!~, :==, :===, :=~, : **id** , : **send** , :class, :clone,  
:define\_si  
ngleton\_method, :display, :dup, :enum\_for, :eql?, :equal?, :extend,  
:freeze, :fr  
ozen?, :gem, :hash, :inspect, :instance\_eval, :instance\_exec,  
:instance\_of?, :in  
stance\_variable\_defined?, :instance\_variable\_get,  
:instance\_variable\_set, :insta  
nce\_variables, :is\_a?, :kind\_of?, :method, :methods, :nil?, :object\_id,  
:private  
\_methods, :protected\_methods, :public\_method, :public\_methods,  
:public\_send, :re  
spond\_to?, :send, :singleton\_methods, :taint, :tainted?, :tap, :to\_enum,  
:to\_s,  
:trust, :untaint, :untrust, :untrusted?]  
irb(main):007:0\>

Maybe I’m missing something?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 20, 2009, 11:51pm UTC](https://www.ruby-forum.com/t/something-like-puts/179268/7 "2009-11-20T23:51:04Z")

</div>

> -----Original Message-----  
> From: Walton H. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> 
> > -----Original Message-----  
> > From: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> > 
> > Right. Which actually puts it in Object. I had forgotten about the  
> > syntactic sugar.
> 
> Does it? I’ve heard that before, but this seems to indicate otherwise.  
> [snip]  
> Maybe I’m missing something?

I am. To answer my own foolish question:

irb(main):021:0\> Object.private\_methods.sort  
=\>[#snip# :foobar #snip#]  
irb(main):022:0\>

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 20, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/something-like-puts/179268/8 "2009-11-20T20:43:12Z")

</div>

> > I think this has something to do with Modules not mixing in, uh, class  
> > methods. Frankly, I’m not following the arguments.
> > 
> > So … what is the right way to create a function like puts that is  
> > known everywhere?

WH\> Maybe I’m missing something, but I think what you want is to just  
WH\> define it in the default global scope:

Duh! That works!

God, I’m stupid. Thanks!!!

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 21, 2009, 12:25am UTC](https://www.ruby-forum.com/t/something-like-puts/179268/9 "2009-11-21T00:25:01Z")

</div>

Hi –

On Sat, 21 Nov 2009, Walton H. wrote:

> [snip]
> 
> > Maybe I’m missing something?
> 
> I am. To answer my own foolish question:
> 
> irb(main):021:0\> Object.private\_methods.sort  
> =\>[#snip# :foobar #snip#]  
> irb(main):022:0\>

Mind you:

irb(main):010:0\> def x; end  
=\> nil  
irb(main):011:0\> “some string”.private\_methods.grep(/^x/)  
=\> [:x]

It becomes a private instance method of every object, including the  
class object Object, because it’s defined inside Object:

irb(main):013:0\> Object.private\_instance\_methods(false).grep(/^x/)  
=\> [:x]

David
