# Another require question

**URL:** <https://www.ruby-forum.com/t/another-require-question/152314>\
**Category:** Ruby\
**Created:** [November 1, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/another-require-question/152314 "2008-11-01T22:58:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![devros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7c8e57/32.png) [@devros](https://www.ruby-forum.com/u/devros)\
**Post date:** [November 1, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/another-require-question/152314/1 "2008-11-01T22:58:59Z")

</div>

I’ve been learning Ruby for 2 weeks and of course I like it.

However, recently,

1. 

‘file.open(path)’ returns this error in the SciTE editor:

“undefined local variable or method `math’ for main:Object (NameError)”

1. 

I try to require ‘file’ and fail at that too, even with an absolute path  
to ‘file.rb’ under the gems folder.

Maybe I need to reinstall Ruby?

Help please.

---

<div class="post-metadata">

**Author:** ![devros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7c8e57/32.png) [@devros](https://www.ruby-forum.com/u/devros)\
**Post date:** [November 1, 2008, 11:09pm UTC](https://www.ruby-forum.com/t/another-require-question/152314/2 "2008-11-01T23:09:32Z")

</div>

Ed Hardy wrote:

> file.open(path)’ returns this error in the SciTE editor:
> 
> “undefined local variable or method `math’ for main:Object (NameError)”

That line most certainly does not cause that error. You might want to  
show the  
whole code or if it’s a lot of code, the smallest possible example  
that’s  
runnable and produces the error.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![devros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7c8e57/32.png) [@devros](https://www.ruby-forum.com/u/devros)\
**Post date:** [November 2, 2008, 12:18am UTC](https://www.ruby-forum.com/t/another-require-question/152314/3 "2008-11-02T00:18:46Z")

</div>

Thanks Sebastian, and apologies for recent goof. Again…

This single line,

file.exist?(“c:/xx.txt”)

or any file.xxx() command,  
when run, produces in the error window:

miscmo.rb:2: undefined local variable or method `file’ for main:Object  
(NameError)

I reinstalled in windows using the installer ruby186-26.exe. No  
improvement.

---

<div class="post-metadata">

**Author:** ![devros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7c8e57/32.png) [@devros](https://www.ruby-forum.com/u/devros)\
**Post date:** [November 2, 2008, 12:36am UTC](https://www.ruby-forum.com/t/another-require-question/152314/4 "2008-11-02T00:36:09Z")

</div>

Yike. I ‘knew it’ but forgot, not at the surface of my mind/reading. I  
had fallen into a misconception, explained by this correct  
interpretation of file handing…

ruby\> begin  
ruby| file = open("/tmp/some\_file", “w”)  
ruby| # something to do  
ruby| ensure  
ruby| file.close  
ruby| end

Sebastian H. wrote:

> Ed Hardy wrote:
> 
> > file.open(path)’ returns this error in the SciTE editor:
> > 
> > “undefined local variable or method `math’ for main:Object (NameError)”
> 
> That line most certainly does not cause that error. You might want to  
> show the  
> whole code or if it’s a lot of code, the smallest possible example  
> that’s  
> runnable and produces the error.
> 
> HTH,  
> Sebastian

---

<div class="post-metadata">

**Author:** ![devros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7c8e57/32.png) [@devros](https://www.ruby-forum.com/u/devros)\
**Post date:** [November 7, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/another-require-question/152314/5 "2008-11-07T20:04:38Z")

</div>

Let’s assume I know nothing. Though I thought I understood this  
dilemma, I’m now lost. I’ve been studying like crazy, bought a book,  
and even have even written a program that pumps out html pages with  
webrick, but I am still hung up on some basic items.

For instance, this line fails:

puts math.sqrt(234)

it outputs this error:

“undefined local variable or method `math’ for main:Object (NameError)”

this line fails:

f= file.open(‘c:\cher.bat’) # similar error message as above

these lines run OK:

f= open(‘c:\cher.bat’)  
d= f.read()  
puts d  
f.close()

I installed 1.8.6 (path level 111) on XP Windows with the one-click  
installer.

---

<div class="post-metadata">

**Author:** ![devros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7c8e57/32.png) [@devros](https://www.ruby-forum.com/u/devros)\
**Post date:** [November 7, 2008, 8:14pm UTC](https://www.ruby-forum.com/t/another-require-question/152314/6 "2008-11-07T20:14:43Z")

</div>

On Fri, Nov 7, 2008 at 11:02 AM, Ed Hardy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Let’s assume I know nothing. Though I thought I understood this  
> dilemma, I’m now lost. I’ve been studying like crazy, bought a book,  
> and even have even written a program that pumps out html pages with  
> webrick, but I am still hung up on some basic items.
> 
> For instance, this line fails:
> 
> puts math.sqrt(234)

Same issue - class and module names are capitalised in ruby.

Math.sqrt  
File.open

etc

martin

---

<div class="post-metadata">

**Author:** ![devros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7c8e57/32.png) [@devros](https://www.ruby-forum.com/u/devros)\
**Post date:** [November 2, 2008, 12:32am UTC](https://www.ruby-forum.com/t/another-require-question/152314/7 "2008-11-02T00:32:43Z")

</div>

Ed Hardy wrote:

> (NameError)
> 
> I reinstalled in windows using the installer ruby186-26.exe. No  
> improvement.

Try

File.exist?(“c:/xx.txt”)

exist? is a class method of the class File

---

<div class="post-metadata">

**Author:** ![devros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7c8e57/32.png) [@devros](https://www.ruby-forum.com/u/devros)\
**Post date:** [November 7, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/another-require-question/152314/8 "2008-11-07T20:17:46Z")

</div>

Ed Hardy wrote:  
(snip)

> For instance, this line fails:
> 
> puts math.sqrt(234)
> 
> it outputs this error:
> 
> “undefined local variable or method `math’ for main:Object (NameError)”
> 
> this line fails:
> 
> f= file.open(‘c:\cher.bat’) # similar error message as above  
> (snip)

In Ruby, case matters. It’s Math.sqrt, not math.sqrt, and File.open, not  
file.open.

In Ruby, variables that start with an uppercase character are constants,  
and the names of modules (like Math) and classes (like File) are  
constants.

Variables that start with a lowercase character are just plain  
variables.

---

<div class="post-metadata">

**Author:** ![devros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7c8e57/32.png) [@devros](https://www.ruby-forum.com/u/devros)\
**Post date:** [November 7, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/another-require-question/152314/9 "2008-11-07T20:20:22Z")

</div>

Thanks Martin, and all!!
