where does the pure method defined when starting irb

B

Brian Xue

[Note: parts of this message were removed to make it a legal post.]

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
 
J

jake kaiden

hi -

try this:


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

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

cheers,

-j
 
C

Christopher Dicely

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]
christopher@ubuntu:~$ 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:

christopher@ubuntu:~$ ruby -v
ruby 1.9.3dev (2011-05-02 trunk 31407) [x86_64-linux]
christopher@ubuntu:~$ 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
 
M

Michael Edgar

[Note: parts of this message were removed to make it a legal post.]



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]
christopher@ubuntu:~$ 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:

christopher@ubuntu:~$ ruby -v
ruby 1.9.3dev (2011-05-02 trunk 31407) [x86_64-linux]
christopher@ubuntu:~$ 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) .
 
7

7stud --

puts RUBY_VERSION

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

def hello
end

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

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

7stud --

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)
 
7

7stud --

Christopher Dicely wrote in post #998250:
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


===
 
B

Brian Xue

[Note: parts of this message were removed to make it a legal post.]

Thanks all for your kindly explanation. It really helps.

Brian
 
7

7stud --

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 method `instance_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
 
7

7stud --

Brian Xue 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:

Object
^ private :greet
|
|
Class B
^
|
|
singleton class of b
^
|
|
b = B.new





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

Brian Xue

[Note: parts of this message were removed to make it a legal post.]

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
 
M

Michael Edgar

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
=3D> main
def hello
@v =3D 1
end
=3D> nil
puts self.instance_variables
=3D> nil
hello
=3D> 1
puts self.instance_variables
=3D> @v
=3D> [:mad:v]

Michael Edgar
(e-mail address removed)
http://carboni.ca/
 
B

Brian Xue

[Note: parts of this message were removed to make it a legal post.]

Thank you very much, Michael!

Brian

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
=> [:mad:v]

Michael Edgar
(e-mail address removed)
http://carboni.ca/

Can anyone help explain?

Many thanks!

Brian
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top