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

···

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

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

···

On Thu, May 12, 2011 at 2:40 AM, Brian Xue <brxue.cn@gmail.com> 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

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

···

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

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)

···

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

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

···

On May 12, 2011, at 10:57 AM, Christopher Dicely <cmdicely@gmail.com> wrote:

On Thu, May 12, 2011 at 2:40 AM, Brian Xue <brxue.cn@gmail.com> wrote:

Christopher Dicely wrote in post #998250:

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

···

On Thu, May 12, 2011 at 2:40 AM, Brian Xue <brxue.cn@gmail.com> wrote:

===

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

Thanks all for your kindly explanation. It really helps.

Brian

···

2011/5/13 7stud -- <bbxx789_05ss@yahoo.com>

Christopher Dicely wrote in post #998250:
> On Thu, May 12, 2011 at 2:40 AM, Brian Xue <brxue.cn@gmail.com> 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

===

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

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

···

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

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.

···

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

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

···

On Fri, May 13, 2011 at 10:07 AM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

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.

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

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 Edgar
adgar@carboni.ca
http://carboni.ca/

···

On May 16, 2011, at 8:54 PM, Brian Xue wrote:

Can anyone help explain?

Many thanks!

Brian

Thank you very much, Michael!

Brian

···

On Tue, May 17, 2011 at 9:00 AM, Michael Edgar <adgar@carboni.ca> wrote:

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 Edgar
adgar@carboni.ca
http://carboni.ca/

On May 16, 2011, at 8:54 PM, Brian Xue wrote:

> Can anyone help explain?
>
> Many thanks!
>
> Brian