Top level function definition gets in Kernel module?

irb(main):001:0> def test_fun
irb(main):002:1> end
=> nil
irb(main):003:0> p Object.instance_methods.sort
[...]
singleton_methods", "taint", "tainted?", "tap", "test_fun", "to_a",
"to_enum", "to_s", "type", "untaint"]
=> nil
irb(main):004:0> p Kernel.methods.sort
[...]
"tainted?", "tap", "test", "test_fun", "throw", "to_a", "to_enum",
"to_s", "trace_var", "trap", "type", "untaint", "untrace_var", "warn"]
=> nil

So I understand that test_fun definition refers to the current class
which is Object and is added to the instance_methods but I don't
understand why this also appears in the Kernel module methods.

Any ideas?

Thanks,

···

--
Posted via http://www.ruby-forum.com/.

You wrote Kernel.methods, not Kernel.instance_methods. Kernel.methods
returns the names of all the methods on the Kernel object itself. "Kernel" is
a reference to an object of class Module, which is a subclass of Object.

Thus, since test_fun is an instance method on Object, and Kernel is an instance
of a subclass of Object, it has access to that method as well.

I think you were confused between Kernel.methods and Kernel.instance_methods.
Kernel.instance_methods does not include "test_fun" given this example, as expected.

Michael Edgar
adgar@carboni.ca
http://carboni.ca/

···

On Jul 23, 2011, at 4:57 PM, Jean-Pascal Billaud wrote:

irb(main):001:0> def test_fun
irb(main):002:1> end
=> nil
irb(main):003:0> p Object.instance_methods.sort
[...]
singleton_methods", "taint", "tainted?", "tap", "test_fun", "to_a",
"to_enum", "to_s", "type", "untaint"]
=> nil
irb(main):004:0> p Kernel.methods.sort
[...]
"tainted?", "tap", "test", "test_fun", "throw", "to_a", "to_enum",
"to_s", "trace_var", "trap", "type", "untaint", "untrace_var", "warn"]
=> nil

Almost everything in ruby is an object. All objects inherit from
Object. Kernel is a class, but Kernel itself is also an object, so it,
like all objects, inherits from Object. Therefore, all instance methods
in Object are inherited by Kernel

Note the circularity: Object includes Kernel, so all the Kernel methods
are instance methods of Object. As noted above, Kernel inherits from
Object, so Kernel actually inherits all its own instance methods.

···

--
Posted via http://www.ruby-forum.com/.

Aargh.

$$ head -n6 ruby.rb
def test_fun
end

puts Object.instance_methods.grep(/^t/).sort
puts '-' * 20
puts Kernel.methods.grep(/^t/).sort

$multiruby ruby.rb
VERSION = 1.8.6-p420
CMD = ~/.multiruby/install/1.8.6-p420/bin/ruby ruby.rb

taint
tainted?
to_a
to_s
type

···

--------------------
taint
tainted?
test
throw
to_a
to_s
trace_var
trap
type

RESULT = pid 92608 exit 0

VERSION = 1.8.7-p352
CMD = ~/.multiruby/install/1.8.7-p352/bin/ruby ruby.rb

taint
tainted?
tap
to_a
to_enum
to_s
type
--------------------
taint
tainted?
tap
test
throw
to_a
to_enum
to_s
trace_var
trap
type

RESULT = pid 92609 exit 0

VERSION = 1.9.1-p431
CMD = ~/.multiruby/install/1.9.1-p431/bin/ruby ruby.rb

taint
tainted?
tap
to_enum
to_s
trust
--------------------
taint
tainted?
tap
test
throw
to_enum
to_s
trace_var
trap
trust

RESULT = pid 92610 exit 0

VERSION = 1.9.2-p290
CMD = ~/.multiruby/install/1.9.2-p290/bin/ruby ruby.rb

taint
tainted?
tap
to_enum
to_s
trust
--------------------
taint
tainted?
tap
test
throw
to_enum
to_s
trace_var
trap
trust

RESULT = pid 92611 exit 0

TOTAL RESULT = 0 failures out of 4

Passed: 1.8.6-p420, 1.8.7-p352, 1.9.1-p431, 1.9.2-p290
Failed:

--
Posted via http://www.ruby-forum.com/.

Thanks everyone for the explanation, I just got confused and forgot that
Kernel was also an object...

BTW, any reason for the change in later version to have method
definition at the top-level scope being created in the private methods
space. That only seems specific to top-level scope. I am just wondering
about why the special case here...

thanks,

···

--
Posted via http://www.ruby-forum.com/.

The top-level scope creates private methods by default.

Object.private_instance_methods is what you want.

Michael Edgar
adgar@carboni.ca
http://carboni.ca/

···

On Jul 23, 2011, at 5:52 PM, 7stud -- wrote:

Aargh.

Michael Edgar wrote in post #1012619:

Aargh.

The top-level scope creates private methods by default.

I know that.

Object.private_instance_methods is what you want.

I don't want anything.

···

On Jul 23, 2011, at 5:52 PM, 7stud -- wrote:

--
Posted via http://www.ruby-forum.com/\.

The top-level scope creates private methods by default.

I know that.

Then why did you add the "test_fun" private method to Object,
then print Object's public instance methods starting with t?

Object.private_instance_methods is what you want.

I don't want anything.

If your printout had nothing to do with the "test_fun" method, then all
it seemed to illustrate was that different versions of Ruby provide
different methods. Was there something else to be gleaned that
I'm missing?

Michael Edgar
adgar@carboni.ca
http://carboni.ca/

···

On Jul 23, 2011, at 7:23 PM, 7stud -- wrote: