Where does the pure method defined when starting irb

Hello,

I have some puzzles, when I start irb,

puts self
=> main
puts self.class
=> Object

After that, if I write the following method,

def hello; end
=>nil

then where is the method “hello” defined? within Object? but

self.class.instance_methods.include? :hello
=>false

Can anyone help explain that?

Thanks in advance!

Brian

hi -

try this:

irb(main):001:0> def hello; end

irb(main):002:0> self.public_methods.include?(“hello”)
=> true

cheers,

-j

On Thu, May 12, 2011 at 2:40 AM, Brian X. [email protected] wrote:

def hello; end
=>nil

then where is the method “hello” defined? within Object? but

self.class.instance_methods.include? :hello
=>false

Right, because its not defined as an instance method in the Object
class, its defined as an instance method in the singleton class of the
current object (main).

So, in IRB for Ruby 1.8.7 after the above

(class <<self; end).instance_methods.include? “hello”
=> true

For some reason, IRB for Ruby 1.9.2 is weird, and I can’t find the
method anywhere, even though the method works. Even respond_to?
ignores it:

ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
[email protected]:~$ irb
ruby-1.9.2-p180 :001 > def hello
ruby-1.9.2-p180 :002?> “hello”
ruby-1.9.2-p180 :003?> end
=> nil
ruby-1.9.2-p180 :004 > hello
=> “hello”
ruby-1.9.2-p180 :005 > self.respond_to? :hello
=> false

Its back to working again in head, which shows the cleaner 1.9 syntax:

[email protected]:~$ ruby -v
ruby 1.9.3dev (2011-05-02 trunk 31407) [x86_64-linux]
[email protected]:~$ irb
ruby-head :001 > def hello
ruby-head :002?> “hello”
ruby-head :003?> end
=> nil
ruby-head :004 > hello
=> “hello”
ruby-head :005 > self.respond_to? :hello
=> true
ruby-head :006 > self.singleton_class.instance_methods.include? :hello
=> true

puts RUBY_VERSION #=>1.9.2

puts self #=>main
puts self.class #=>Object

def hello
end

puts Object.private_methods.grep(/^h/) #=>hello

puts self.public_methods.include?(“hello”)
#=>false

puts self.singleton_class.instance_methods.include?(:hello)
#=>false

According to ‘Metaprogramming Ruby’, all def statements attach
themselves to the ‘current class’, and at the toplevel the current class
is Object.

Somehow toplevel methods end up being private methods of the Object
class, which means you can call them anywhere–but you can’t use a
receiver.

Also:

puts RUBY_VERSION #=>1.8.6

puts self #=>main
puts self.class #=>Object

def hello
end

puts Object.private_instance_methods.grep(/^h/) #=>hello

puts self.public_methods.include?(“hello”)
#=>false

#puts self.singleton_class.instance_methods.include?(:hello)

On May 12, 2011, at 10:57 AM, Christopher D. [email protected]
wrote:

On Thu, May 12, 2011 at 2:40 AM, Brian X. [email protected] wrote:

Hello,

I have some puzzles, when I start irb,

puts self

=> main

puts self.class

=> Object

After that, if I write the following method,

def hello; end

=>nil

then where is the method “hello” defined? within Object? but

self.class.instance_methods.include? :hello

=>false

Right, because its not defined as an instance method in the Object
class, its defined as an instance method in the singleton class of the
current object (main).

So, in IRB for Ruby 1.8.7 after the above

(class <<self; end).instance_methods.include? “hello”
=> true

For some reason, IRB for Ruby 1.9.2 is weird, and I can’t find the
method anywhere, even though the method works. Even respond_to?
ignores it:

ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
[email protected]:~$ irb
ruby-1.9.2-p180 :001 > def hello
ruby-1.9.2-p180 :002?> “hello”
ruby-1.9.2-p180 :003?> end
=> nil
ruby-1.9.2-p180 :004 > hello
=> “hello”
ruby-1.9.2-p180 :005 > self.respond_to? :hello
=> false

Its back to working again in head, which shows the cleaner 1.9 syntax:

[email protected]:~$ ruby -v
ruby 1.9.3dev (2011-05-02 trunk 31407) [x86_64-linux]
[email protected]:~$ irb
ruby-head :001 > def hello
ruby-head :002?> “hello”
ruby-head :003?> end
=> nil
ruby-head :004 > hello
=> “hello”
ruby-head :005 > self.respond_to? :hello
=> true
ruby-head :006 > self.singleton_class.instance_methods.include? :hello
=> true

Methods defined at the top level are created as a private instance
method on
the Object class. You can use public/private at the top level to change
the
visibility used. In this case, try
Object.private_instance_methods(false) .

7stud – wrote in post #998328:

Also:

puts RUBY_VERSION

x = class A
end

p x

x = class <<self
end

p x

–output:–
1.9.2
nil
nil

p x.instance_methods

prog.rb:11:in <main>': undefined methodinstance_methods’ for
nil:NilClass (NoMethodError)

And…

puts RUBY_VERSION #=>1.8.6

x = class A
end

p x #=>nil

x = class <<self
end

p x #=>nil

p x.instance_methods #error

Thanks all for your kindly explanation. It really helps.

Brian

2011/5/13 7stud – [email protected]

Christopher D. wrote in post #998250:

On Thu, May 12, 2011 at 2:40 AM, Brian X. [email protected] wrote:

def hello; end
=>nil

then where is the method “hello” defined? within Object? but

self.class.instance_methods.include? :hello
=>false

Right, because its not defined as an instance method in the Object
class, its defined as an instance method in the singleton class of the
current object (main).

So, in IRB for Ruby 1.8.7 after the above

(class <<self; end).instance_methods.include? “hello”
=> true

Also:

puts RUBY_VERSION

x = class A
end

p x

x = class <<self
end

p x

–output:–
1.9.2
nil
nil

p x.instance_methods

prog.rb:11:in <main>': undefined methodinstance_methods’ for
nil:NilClass (NoMethodError)

Brian X. wrote in post #998397:

Thanks all for your kindly explanation. It really helps.

Here’s more:

class Object
private

def greet
puts ‘hello’
end
end

class B
end

puts Object.private_instance_methods.include?(:greet)
puts B.private_instance_methods.include?(:greet)
puts self.singleton_class.private_instance_methods.include?(:greet)

not_inherited = false
puts
self.singleton_class.private_instance_methods(not_inherited).include?(:greet)

–output:–
true
true
true
false

The lookup paths:

class BasicObject(in Ruby 1.9)
^
|
|
class Object
^ private :greet
|
|
class B
^
|
|
singleton class of b
^
|
|
b = B.new

class Object
^ private :greet
|
|
singleton class of ‘main’
^
|
|
main

You really can’t determine in which class a method is defined unless you
call the *_methods() with false as the argument, which causes ruby to
ignore inherited methods.

An instance variable doesn’t get created until it is first assigned. So
in this case, you
have to call hello before @v will show up in
self.instance_variables:

puts self
=> main
def hello
@v = 1
end
=> nil
puts self.instance_variables
=> nil
hello
=> 1
puts self.instance_variables
=> @v
=> [:@v]

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

Thank you all.

Here I have another similar question:

puts self
=> main
def hello;
@v = 1;
end
=>nil

then I assume @v should be an instance variable of ‘main’, but

puts self.instance_variables
=>nil

Can anyone help explain?

Many thanks!

Brian

Thank you very much, Michael!

Brian

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs