Something like puts

R

Ralph Shnelvar

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.


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

Marnen Laibow-Koser

Ralph said:
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,
 
W

Walton Hoops

From: Ralph Shnelvar [mailto:[email protected]]
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.


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

David Masover

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

Ralph Shnelvar

From: Ralph Shnelvar [mailto:[email protected]]

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

Marnen Laibow-Koser

Walton said:
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,
 
W

Walton Hoops

-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]]
Walton said:
Maybe I'm missing something, but I think what you want is to just
define it in the default global scope:
[...]
=20
Right. Which actually puts it in Object. I had forgotten about the
syntactic sugar.
=20

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
=3D> nil
irb(main):004:0> Object.methods.sort
=3D> [:!, :!=3D, :!~, :<, :<=3D, :<=3D>, :=3D=3D, :=3D=3D=3D, :=3D~, :>, =
:>=3D, :__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?, :eek:bject_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 =3D Object.new
=3D> #<Object:0x2add480>
irb(main):006:0> o.methods.sort
=3D> [:!, :!=3D, :!~, :=3D=3D, :=3D=3D=3D, :=3D~, :__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?, :eek:bject_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?
 
W

Walton Hoops

-----Original Message-----
From: Walton Hoops [mailto:[email protected]]
-----Original Message-----
From: (e-mail address removed) [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>
 
D

David A. Black

Hi --

-----Original Message-----
From: Walton Hoops [mailto:[email protected]]
-----Original Message-----
From: (e-mail address removed) [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>

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top