From: Bryan JJ Buckley Date: 2008-05-30T01:06:53+09:00 Subject: Re: Constants in modules --------------040406020506050201090206 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Two things - firstly, Ruby is interpreted, so invert_hash needs to be declared *before* you use it. Then, your methods need to be module methods - just put a "self." before each one, and it should work. Luis Crespo wrote: > Hello, > > I need to do something like this (this is a simplified version of some > other code, of course, but I don't want to get you lost in details > unrelated to the problem): > > module M > > CONST_A = { 'k1' => 'v1', 'k2' => 'v2' } > CONST_B = invert_hash(CONST_A) > > def method1 > # Do something with CONST_A > puts CONST_A > end > > def method2 > # Do something with CONST_B > puts CONST_B > end > > def invert_hash(h) > inverted = {} > h.each_pair { |k, v| inverted[v] = k } > inverted > end > > end > > My intention is to be able to calculate CONST_B by invoking a method in > the module itself... but I get this error message: > > tests.rb:4: undefined method `invert_hash' for M:Module (NoMethodError) > from tests.rb:1 > > I have tried lots of combinations by prefixing the module name, using > ::, using module_function, etc., but nothing has worked. > > Is there a way to do this? Of course I know there are obvious > workarounds such as defining the method outside the module, but that > would be quite ugly... and there should be a way to use code in my own > module from my own module!!! > > Thanks in advance for your help, > > Luis. > --------------040406020506050201090206 Content-Type: text/x-vcard; charset=utf-8; name="jj.vcf" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="jj.vcf" YmVnaW46dmNhcmQNCmZuOkpKIEJ1Y2tsZXkNCm46QnVja2xleTtKSg0Kb3JnOlRyYXZlbCBJ USBHbWJIDQphZHI6Ozs7Ozs7RGV1dHNjaGxhbmQNCmVtYWlsO2ludGVybmV0OmpqQHRyYXZl bGlxLmRlDQp0ZWw7d29yazorNDkzMDY5MjA1MTI5Mg0KdGVsO2ZheDorNDkzMDY5MjA1MTI5 OQ0KdGVsO2NlbGw6KzQ5MTczMTc0NDE3NQ0KeC1tb3ppbGxhLWh0bWw6VFJVRQ0KdmVyc2lv bjoyLjENCmVuZDp2Y2FyZA0KDQo= --------------040406020506050201090206--