From: Rein Henrichs Date: 2010-06-14T12:30:06+09:00 Subject: Re: NoMethodError, most likely foolish... On 2010-06-13 20:06:02 -0700, Adam Cohen said: > So I am fairly new to ruby and I'm using the language to work on a new > project. The details of the project however are not relevant to this > post, but some of the code is. > > Long story short, I have a big case statement that will execute > different methods depending on input (its a command line system im > building for sometihng) and the case will call my method version_info? > by using the call self.version_info? however I then get this error > instead of the proper output. > > secretproject.rb:51: undefined method `version_info?' for main:Object > (NoMethodError) > > > my method looks like this > > def version_info? > puts 'secretproject runtime version ' + version.to_s > puts 'written and maintained: Adam Ross Cohen' > puts 'math genius: Evan Penn' > puts 'thanks for playing.' > end > > I dont understand why the object doesn't contain the method, however I > have a hunch... my ruby file isn't a class, its just a file with methods > in it, however as I understand it I should still be able to call the > method because it makes the object when it runs the file. > > please let me know if you can correct my (most likely foolish) mistake. > thanks in advance, A. Ross Cohen Methods defined in the global scope (on the object that Ruby calls "main") are private and will raise a NoMethodError in newer versions of Ruby when called with an explicit receiver, even if that receiver is self. However, that error will say "NoMethodError: private method `version_info?' called for ..." rather than "undefined method", so that is not the current problem. Given the actual error raised, the code you've provided is insufficient to determine the cause. Try to reduce your code to the minimum required to demonstrate the error. If the solution isn't clear to you at that point, try posting that code again here. As an aside, a query method (one that ends in a "?") is not the correct place to put side-effect driven code (code that writes to STDOUT, for instance). You should call your method "version_info" and reserve methods that end in "?" for their normal use: queries that are used to determine the truthiness of a question, like [].empty? and "foo".nil? -- Rein Henrichs http://puppetlabs.com http://reinh.com