From: Marc Heiler Date: 2011-10-21T20:10:17+09:00 Subject: When is a method known to Ruby? Hi. When is a method "known" to the ruby parser and why? Example 1, works: require 'pp' module Foo def self.foo(i) # i is a string that will be shortened. return i.chop end CONSTANT_1 = 'bla' CONSTANT_2 = foo('bla') end constants = Foo.constants pp Foo.foo 'abc' pp Foo::CONSTANT_2 pp constants # This works, returns: # "ab" # "bl" # ["CONSTANT_1", "CONSTANT_2"] Example 2, does not work: require 'pp' module Foo CONSTANT_1 = 'bla' CONSTANT_2 = foo('bla') def self.foo(i) # i is a string that will be shortened. return i.chop end end constants = Foo.constants pp Foo.foo 'abc' pp Foo::CONSTANT_2 pp constants # This does not work, returns: # module_test.rb:6: undefined method `foo' for Foo:Module (NoMethodError) The difference is when def self.foo(i) appears. Apparently the parser must have "seen" it before working on it. Can someone explain to me why, and if there is another workaround other than moving the method definition before the method call first happens? -- Posted via http://www.ruby-forum.com/.