Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4344 > unrolled thread
| Started by | Brian Xue <brxue.cn@gmail.com> |
|---|---|
| First post | 2011-05-12 04:40 -0500 |
| Last post | 2011-05-12 13:53 -0500 |
| Articles | 13 — 5 participants |
Back to article view | Back to comp.lang.ruby
where does the pure method defined when starting irb Brian Xue <brxue.cn@gmail.com> - 2011-05-12 04:40 -0500
Re: where does the pure method defined when starting irb jake kaiden <jakekaiden@yahoo.com> - 2011-05-12 06:17 -0500
Re: where does the pure method defined when starting irb Christopher Dicely <cmdicely@gmail.com> - 2011-05-12 09:57 -0500
Re: where does the pure method defined when starting irb Michael Edgar <adgar@carboni.ca> - 2011-05-12 10:12 -0500
Re: where does the pure method defined when starting irb 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 14:00 -0500
Re: where does the pure method defined when starting irb Brian Xue <brxue.cn@gmail.com> - 2011-05-12 18:57 -0500
Re: where does the pure method defined when starting irb 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 21:07 -0500
Re: where does the pure method defined when starting irb Brian Xue <brxue.cn@gmail.com> - 2011-05-16 19:54 -0500
Re: where does the pure method defined when starting irb Michael Edgar <adgar@carboni.ca> - 2011-05-16 20:00 -0500
Re: where does the pure method defined when starting irb Brian Xue <brxue.cn@gmail.com> - 2011-05-16 21:24 -0500
Re: where does the pure method defined when starting irb 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 18:58 -0500
Re: where does the pure method defined when starting irb 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 13:21 -0500
Re: where does the pure method defined when starting irb 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 13:53 -0500
| From | Brian Xue <brxue.cn@gmail.com> |
|---|---|
| Date | 2011-05-12 04:40 -0500 |
| Subject | where does the pure method defined when starting irb |
| Message-ID | <BANLkTi=rQN7E75h4zWxOi8WdhqD_reBbAA@mail.gmail.com> |
[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
[toc] | [next] | [standalone]
| From | jake kaiden <jakekaiden@yahoo.com> |
|---|---|
| Date | 2011-05-12 06:17 -0500 |
| Message-ID | <fbaa9b66f800b14e38c2e3e3871a1d1f@ruby-forum.com> |
| In reply to | #4344 |
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/.
[toc] | [prev] | [next] | [standalone]
| From | Christopher Dicely <cmdicely@gmail.com> |
|---|---|
| Date | 2011-05-12 09:57 -0500 |
| Message-ID | <BANLkTinXr6CBdY+5Q=_iMtUheG3EZ9GoCA@mail.gmail.com> |
| In reply to | #4344 |
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 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
[toc] | [prev] | [next] | [standalone]
| From | Michael Edgar <adgar@carboni.ca> |
|---|---|
| Date | 2011-05-12 10:12 -0500 |
| Message-ID | <6771931668916090376@unknownmsgid> |
| In reply to | #4371 |
[Note: parts of this message were removed to make it a legal post.] 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: 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) .
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-12 14:00 -0500 |
| Message-ID | <b6130c2d57835d754af54499c4503921@ruby-forum.com> |
| In reply to | #4371 |
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/.
[toc] | [prev] | [next] | [standalone]
| From | Brian Xue <brxue.cn@gmail.com> |
|---|---|
| Date | 2011-05-12 18:57 -0500 |
| Message-ID | <BANLkTim+dK3dSgmJ=xMfbGK_UDSXOw_wyg@mail.gmail.com> |
| In reply to | #4414 |
[Note: parts of this message were removed to make it a legal post.] 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/. > >
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-12 21:07 -0500 |
| Message-ID | <b8c302fbff98c1f2edefabb50e688446@ruby-forum.com> |
| In reply to | #4442 |
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/.
[toc] | [prev] | [next] | [standalone]
| From | Brian Xue <brxue.cn@gmail.com> |
|---|---|
| Date | 2011-05-16 19:54 -0500 |
| Message-ID | <BANLkTinr1WA8c+fdm7w-2QGgUx-BQUwsKw@mail.gmail.com> |
| In reply to | #4455 |
[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 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/. > >
[toc] | [prev] | [next] | [standalone]
| From | Michael Edgar <adgar@carboni.ca> |
|---|---|
| Date | 2011-05-16 20:00 -0500 |
| Message-ID | <68ED2492-AB01-4697-892E-944865943495@carboni.ca> |
| In reply to | #4649 |
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
[toc] | [prev] | [next] | [standalone]
| From | Brian Xue <brxue.cn@gmail.com> |
|---|---|
| Date | 2011-05-16 21:24 -0500 |
| Message-ID | <BANLkTikXxSyjHvoaTVek2fUf-=TiuGjgRQ@mail.gmail.com> |
| In reply to | #4651 |
[Note: parts of this message were removed to make it a legal post.] 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 > > >
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-12 18:58 -0500 |
| Message-ID | <a852c2acaf8237ef73fafea7fcf48e59@ruby-forum.com> |
| In reply to | #4414 |
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/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-12 13:21 -0500 |
| Message-ID | <e62f34428e5e31da80f2dbe48fa24e13@ruby-forum.com> |
| In reply to | #4344 |
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/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-05-12 13:53 -0500 |
| Message-ID | <ae6bfe7daa8b7bc7ae457165c9b90e56@ruby-forum.com> |
| In reply to | #4344 |
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/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web