to which object does the global scope belong?

D

daz

Daniel said:
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
 
D

Daniel Schüle

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 }
;)
 
D

Daniel Schüle

[...]
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
 
T

Trans

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

Mauricio Fernández

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"
 
T

Trans

Beware of irb.

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

Thanks,
T.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top