Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Something like puts

0 views
Skip to first unread message

Ralph Shnelvar

unread,
Nov 20, 2009, 12:38:19 PM11/20/09
to
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?


Marnen Laibow-Koser

unread,
Nov 20, 2009, 12:51:12 PM11/20/09
to
Ralph Shnelvar 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
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

Walton Hoops

unread,
Nov 20, 2009, 1:08:29 PM11/20/09
to
> From: Ralph Shnelvar [mailto:ral...@dos32.com]


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>


David Masover

unread,
Nov 20, 2009, 2:27:03 PM11/20/09
to
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.

Ralph Shnelvar

unread,
Nov 20, 2009, 2:42:39 PM11/20/09
to


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

Marnen Laibow-Koser

unread,
Nov 20, 2009, 4:23:02 PM11/20/09
to
Walton Hoops 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.

Walton Hoops

unread,
Nov 20, 2009, 5:45:17 PM11/20/09
to
> -----Original Message-----
> From: mar...@marnen.org [mailto:mar...@marnen.org]
> Walton Hoops 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.
>

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?


Walton Hoops

unread,
Nov 20, 2009, 5:50:44 PM11/20/09
to
> -----Original Message-----
> From: Walton Hoops [mailto:wal...@vyper.hopto.org]
> > -----Original Message-----
> > From: mar...@marnen.org [mailto:mar...@marnen.org]
> >
> > 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>


David A. Black

unread,
Nov 20, 2009, 6:12:17 PM11/20/09
to
Hi --

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

--
THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally!
January 22-23, Tampa, Florida
Info and registration at http://www.thecompleatrubyist.com
--------------------------------------
My new job: http://tinyurl.com/yfpn9hz

0 new messages