Query about the top level object

  • Thread starter Gavri Fernandez
  • Start date
G

Gavri Fernandez

Hi everyone,

From the pickaxe first edition:
"At the top level, we're executing code in the context of some
predefined object. When we define methods, we're actually creating
(private) singleton methods for this object."

Is this true? It is true that methods defined at the top-level are
defined as singleton methods of "main" - the top level object? I
thought that the methods added at the top-level are defined as private
methods of Object. (Where I learnt this from, I'm not sure).
Experimenting gave me the latter result.
def whoa
puts "in whoa"
end
puts Object.new.private_methods.find {|x| x == "whoa"}

prints "whoa\n" to the screen.

So is the pickaxe wrong? Or am I missing something big?
 
R

Ryan Leavengood

Gavri said:
Hi everyone,

"At the top level, we're executing code in the context of some
predefined object. When we define methods, we're actually creating
(private) singleton methods for this object."

Is this true? It is true that methods defined at the top-level are
defined as singleton methods of "main" - the top level object? I
thought that the methods added at the top-level are defined as private
methods of Object. (Where I learnt this from, I'm not sure).
Experimenting gave me the latter result.




prints "whoa\n" to the screen.

So is the pickaxe wrong? Or am I missing something big?

It seems to be defined in both:

p self # => main
p self.class # => Object
def foo
p 'foo'
end
p self.private_methods(false) # => ["initialize", "foo", "Rational"]
p Object.new.private_methods(false) # => ["initialize", "foo", "Rational"]
__END__

Hmmm, I need to play around with this some more.

Ryan
 
R

Robert Klemme

Gavri Fernandez said:
Hi everyone,

From the pickaxe first edition:
"At the top level, we're executing code in the context of some
predefined object. When we define methods, we're actually creating
(private) singleton methods for this object."

Is this true? It is true that methods defined at the top-level are
defined as singleton methods of "main" - the top level object? I
thought that the methods added at the top-level are defined as private
methods of Object. (Where I learnt this from, I'm not sure).
Experimenting gave me the latter result.


prints "whoa\n" to the screen.

So is the pickaxe wrong? Or am I missing something big?

This is interesting:

$ ruby -e 'def xxxx() end; [Object.instance_methods, private_methods,
Object.new.methods, Object.new.private_methods].each {|m|
p(m.grep(/xxxx/))}'
[]
["xxxx"]
[]
["xxxx"]
$ ruby -v
ruby 1.8.2 (2004-12-25) [i386-cygwin]

I guess it has something to do with the implementation of
#private_methods...

Kind regards

robert
 
C

Christoph

Ryan said:
It seems to be defined in both:

p self # => main
p self.class # => Object
def foo
p 'foo'
end
p self.private_methods(false) # => ["initialize", "foo",
"Rational"]
p Object.new.private_methods(false) # => ["initialize", "foo",
"Rational"]
__END__

Hmmm, I need to play around with this some more.


Defining a (private) method at the "top" scope is equivalent to
defining a private method at the scope of the class Object. (The same
is true for defining/removing constants by the way ). For this reason
I often felt it might be a good idea idea to get rid of the "main" -
it serves on on apparent use anyway - and just replace it with Object
- that the call at the top scope

--
p self # would results in Object instead of the current"main"
--

---
def Object.method_added(sym)
if private_methods.include?(sym.to_s)
puts "private instance method ##{sym.to_s} was added to class
#{self}"
end
super
end

self.class # => Object
def foo
p 'foo'
end

public

def bla
end


class Object
def blabla
end
private
def bar
end
end
--
resutls in
---
private instance method #foo was added to class Object
private instance method #bar was added to class Object


/Christoph
 

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
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top