To which object does the global scope belong?

x = RubyNG.each {|user| user.hello }.find {|user| user.knows_answer? }

mond:/pool/PROG/ruby # irb
irb(main):001:0>
irb(main):002:0*
irb(main):003:0*
irb(main):004:0* def foobar;"foobar";end
=> nil
irb(main):005:0> self.foobar
=> "foobar"
irb(main):006:0> [].foobar
=> "foobar"
irb(main):007:0> {}.foobar
=> "foobar"
irb(main):008:0> 1.foobar
=> "foobar"
irb(main):009:0> 1.0.foobar
=> "foobar"
irb(main):010:0> Object.foobar
=> "foobar"
irb(main):011:0> Kernel.foobar
=> "foobar"
irb(main):012:0> module M;end
=> nil
irb(main):013:0> M.foobar
=> "foobar"
irb(main):014:0> class X;end
=> nil
irb(main):015:0> X.foobar
=> "foobar"
irb(main):016:0> x=X.new
=> #<X:0x401bcbf0>
irb(main):017:0> x.foobar
=> "foobar"
irb(main):018:0>

I was wondering in what object scope is the function foobar
defined?
I supposed it would be in

irb(main):037:0> Kernel.singleton_methods.grep /foo/
=> []
irb(main):038:0> Object.singleton_methods.grep /foo/
=> []
irb(main):039:0>

but nothing

x.each {|user| user.thanks }
:wink:

Daniel Schüle wrote:

irb(main):004:0* def foobar;"foobar";end
[...]
I was wondering in what object scope is the function foobar
defined?
I supposed it would be in

irb(main):037:0> Kernel.singleton_methods.grep /foo/
=>
irb(main):038:0> Object.singleton_methods.grep /foo/
=>
irb(main):039:0>

It's defined as a private method of the Kernel module.
When mixed-in to class Object, it is an instance method.
Then it's (almost) everywhere.

def foobar;"foobar";end

p Kernel.private_methods.grep(/foo/) # ["foobar"]
p Kernel.private_instance_methods(false).grep(/foo/) #
# (Modules don't have instances)

p Object.private_methods.grep(/foo/) # ["foobar"]
p Object.private_instance_methods(false).grep(/foo/) # ["foobar"]

daz

[...]

def foobar;"foobar";end

p Kernel.private_methods.grep(/foo/) # ["foobar"]
p Kernel.private_instance_methods(false).grep(/foo/) #
# (Modules don't have instances)

mond:/pool/PROG/ruby # irb
irb(main):001:0> def foo;"foo";end
=> nil
irb(main):002:0> Kernel.private_methods.grep /foo/
=>
irb(main):003:0> Kernel.public_methods.grep /foo/
=> ["foo"]
irb(main):004:0>

maybe you meant public_methods?

I noticed that modules may have pub/pro/pri methods
since these methods become instance methods by mix-ing into a class
do these qualifiers provide later visiability to class instance methods?

Regards, Daniel

It becomes a public Object instance method:

irb(main):001:0> def goooooooo; end
=> nil
irb(main):002:0> Object.public_instance_methods(false).sort
=> ["goooooooo", "is_complex_yaml?", "to_yaml", "to_yaml_properties",
"to_yaml_type"]

T.

Beware of irb.

    def gooooo; end
    Object.private_instance_methods(false).sort # => ["gooooo", "initialize"]
    Object.public_instance_methods(false).sort # =>
    RUBY_VERSION # => "1.8.3"

···

On Thu, Nov 10, 2005 at 09:37:13AM +0900, Trans wrote:

It becomes a public Object instance method:

irb(main):001:0> def goooooooo; end
=> nil
irb(main):002:0> Object.public_instance_methods(false).sort
=> ["goooooooo", "is_complex_yaml?", "to_yaml", "to_yaml_properties",
"to_yaml_type"]

T.

--
Mauricio Fernandez

Beware of irb.

Ah. So their *private* instance methods of Object class, then.

Thanks,
T.