Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4375
| From | Michael Edgar <adgar@carboni.ca> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: where does the pure method defined when starting irb |
| Date | 2011-05-12 10:12 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <6771931668916090376@unknownmsgid> (permalink) |
| References | <BANLkTi=rQN7E75h4zWxOi8WdhqD_reBbAA@mail.gmail.com> <BANLkTinXr6CBdY+5Q=_iMtUheG3EZ9GoCA@mail.gmail.com> |
[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) .
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web