Something like puts

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

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
[email protected]

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>

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.

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
[email protected]

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?

-----Original Message-----
From: Walton H. [mailto:[email protected]]

-----Original Message-----
From: [email protected] [mailto:[email protected]]

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>

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

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