# NoMethodError, most likely foolish

**URL:** <https://www.ruby-forum.com/t/nomethoderror-most-likely-foolish/190081>\
**Category:** Ruby\
**Created:** [June 14, 2010, 5:05am UTC](https://www.ruby-forum.com/t/nomethoderror-most-likely-foolish/190081 "2010-06-14T05:05:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![reactorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@reactorman](https://www.ruby-forum.com/u/reactorman)\
**Post date:** [June 14, 2010, 5:05am UTC](https://www.ruby-forum.com/t/nomethoderror-most-likely-foolish/190081/1 "2010-06-14T05:05:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reactorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@reactorman](https://www.ruby-forum.com/u/reactorman)\
**Post date:** [June 14, 2010, 9:52am UTC](https://www.ruby-forum.com/t/nomethoderror-most-likely-foolish/190081/2 "2010-06-14T09:52:13Z")

</div>

Adam C. wrote:

> by using the call self.version\_info? however I then get this error  
> instead of the proper output.
> 
> 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

Don’t use self.version\_info? – just call version\_info?

A file with just this in it works fine:

#---------  
def who\_am\_i?  
puts “I Be Ruby”  
end

who\_am\_i?  
#---------

– greg willits

---

<div class="post-metadata">

**Author:** ![reactorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@reactorman](https://www.ruby-forum.com/u/reactorman)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/nomethoderror-most-likely-foolish/190081/3 "2010-06-17T16:22:31Z")

</div>

On 2010-06-13 20:06:02 -0700, Adam C. said:

> secretproject.rb:51: undefined method `version\_info?’ for main:Object  
> 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?
