[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

def .. end is ok and class .. end is not ok .. Why??

Kyung won Cheon

8/18/2008 7:06:00 AM

module M
end

M.module_eval { def self.a; puts 'ok' end }
M.a
# => 'ok'

M.module_eval { class A; end }
M::A

# => uninitialized constant M::A (NameError)
--
Posted via http://www.ruby-....

1 Answer

Pit Capitain

8/18/2008 7:26:00 AM

0

2008/8/18 Kyung won Cheon <kdream95@gmerce.co.kr>:
> module M
> end
>
> M.module_eval { def self.a; puts 'ok' end }
> M.a
> # => 'ok'
>
> M.module_eval { class A; end }
> M::A
>
> # => uninitialized constant M::A (NameError)

Constants (including class and module names) are lexically scoped (at
least in Ruby 1.8). Your code therefore defines the top-level class A.
If you want a nested constant, you could do

M.module_eval { class self::A; end }

Regards,
Pit