[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Kyung won Cheon

10/22/2008 6:51:00 AM

## in irb

>> self
=> main
>> def why?
>> end
=> nil
>> self.why?
=> nil
>>


## in test.rb

p self # => main
def why?
end
self.why? # private method 'why?' called for main:Object
(NoMethodError)


## Anyway, one more..

## in test2.rb

def why?
end

class A
end

p A.private_method_defined?(:why?) # true


# How is 'why?' inherited???
# The 'main' is a instance of Object but it's not a Object?

# I understand if ...
# class Object
# private
# def why?
# end
# end

##############
# Help Me^^
##############
--
Posted via http://www.ruby-....

3 Answers

Trans

10/22/2008 11:08:00 AM

0



On Oct 22, 2:50=A0am, Kyung won Cheon <kdrea...@gmerce.co.kr> wrote:
> ## in irb
>
>
>
> >> self
> =3D> main
> >> def why?
> >> end
> =3D> nil
> >> self.why?
> =3D> nil
>
> ## in test.rb
>
> p self =A0 =A0 =A0 =A0# =3D> main
> def why?
> end
> self.why? =A0 =A0 # private method 'why?' called for main:Object
> (NoMethodError)
>
> ## Anyway, one more..
>
> ## in test2.rb
>
> def why?
> end
>
> class A
> end
>
> p A.private_method_defined?(:why?) # true
>
> # How is 'why?' inherited???
> # The 'main' is a instance of Object but it's not a Object?
>
> # I understand if ...
> # class Object
> # =A0 private
> # =A0 def why?
> # =A0 end
> # end
>
> ##############
> # Help Me^^
> ##############

The toplevel object (aka 'main') delegates some module-equivalent
methods to Object class. So when you say

def why?
end

What actually happens is:

class Object
def why?
end
private :why?
end

Not all module methods are delegated, try using define_method(:why?)
at the toplevel instead and it will bomb.

T.

P.S. Personally, I find the whole setup rather half-baked, and have
continually advocated for the replacement of the current toplevel
object with a self extended module.

Sebastian Hungerecker

10/22/2008 11:25:00 AM

0

Trans wrote:
> The toplevel object (aka 'main') delegates some module-equivalent
> methods to Object class.

As far as I'm aware it "delegates" only alias, undef, def, module and class.
So it only delegates keywords, not methods. Which is, I assume, why def works
and define_method (which is a method) does not.

HTH,
Sebastian
--
NP: Die Apokalyptischen Reiter - Wir Hoffen
Jabber: sepp2k@jabber.org
ICQ: 205544826

Trans

10/22/2008 7:06:00 PM

0



On Oct 22, 7:25=A0am, Sebastian Hungerecker <sep...@googlemail.com>
wrote:
> Trans wrote:
> > The toplevel object (aka 'main') delegates some module-equivalent
> > methods to Object class.
>
> As far as I'm aware it "delegates" only alias, undef, def, module and cla=
ss.
> So it only delegates keywords, not methods. Which is, I assume, why def w=
orks
> and define_method (which is a method) does not.

Good point. I never really looked at like that b/c I tend to think of
keywords as syntax sugar for real methods.

class -> Class.new
def -> define_method
alias -> alias_method
etc.

Conditionals are an exception, of course.

But it sort of begs the question, why does it support the one and not
the other?

T.