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,

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

“tainted?”, “tap”, “test”, “test_fun”, “throw”, “to_a”, “to_enum”,
“to_s”, “trace_var”, “trap”, “type”, “untaint”, “untrace_var”, “warn”]
=> nil

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 E.
[email protected]
http://carboni.ca/

I don’t
understand why this also appears in the Kernel module methods.

It doesn’t. The Kernel module and the Kernel object itself are two
different things.

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 the Kernel object.

Note the circularity: Object includes the Kernel module, so all the
Kernel methods become instance methods of Object. As noted above, the
Kernel object inherits from
Object, so the Kernel object actually inherits all the instance methods
defined in the Kernel module.

By the way, the results you posted are not true for any recent version
of ruby (and I have no interest in what irb outputs):

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 92571 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 92572 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 92573 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 92574 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:

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

Aargh.

The top-level scope creates private methods by default.

Object.private_instance_methods is what you want.

Michael E.
[email protected]
http://carboni.ca/

Michael E. wrote in post #1012619:

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

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.

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,

Aargh.

Kernel is a class, but Kernel itself is also an object

That should say ‘Kernel is a module, but Kernel itself is also an
object’

$ 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:

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

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 E.
[email protected]
http://carboni.ca/