uninitialized constant NArray (Name Error)

  • Thread starter Abder-Rahman Ali
  • Start date
A

Abder-Rahman Ali

Following section (2) here:
http://ruby.gfd-dennou.org/tutorial/gokuraku/index-e.html

Trying to run this part of script I have so far:

require 'narray'
ary1 = NArray.sfloat(3,4)

I get the following error:


C:\Users\Abder-Rahman\Desktop>ruby narray.rb
/narray.rb:2: uninitialized constant NArray (NameError)
from narray.rb:1:in `require'
from narray.rb:1

How can this issue be solved?

Thanks.
 
B

Brian Candler

Abder-Rahman Ali said:
Following section (2) here:
http://ruby.gfd-dennou.org/tutorial/gokuraku/index-e.html

Trying to run this part of script I have so far:

require 'narray'
ary1 = NArray.sfloat(3,4)

I get the following error:


C:\Users\Abder-Rahman\Desktop>ruby narray.rb
./narray.rb:2: uninitialized constant NArray (NameError)
from narray.rb:1:in `require'
from narray.rb:1

How can this issue be solved?

(1) Don't call your own script 'narray.rb', because then Ruby will think
it has already loaded the narray library.

(2) Install the library narray.rb that you want to use. Probably from
here:
http://narray.rubyforge.org/
 
A

Abder-Rahman Ali

Thanks Brian.

I have renamed the .rb file as: nary.rb

When I run the script, I get the following:

C:\Users\Abder-Rahman\Desktop\Research>ruby nary.rb
nary.rb:1:in `require': no such file to load -- narray (LoadError)
from nary.rb:1

Provided that I have installed the "narray" gem using:

gem install narray

And, just a small thing. When you said: "Don't call your own script
'narray.rb', because then Ruby will think it has already loaded the
narray library."

Can you just clarify this point?

Thanks.
 
A

Abder-Rahman Ali

Thanks Savard. That solved the issue.

Seems we had to do: gem install narray TWICE?!

@Brian, so the name narray.rb can remain as is.

Thanks all.
 
A

Abder-Rahman Ali

@Savard. This works if working through irb.

If I put the script in a .rb file "narray.rb", I get the following:


C:\Users\Abder-Rahman\Desktop\Research>ruby narray.rb
/narray.rb:2: uninitialized constant NArray (NameError)
from narray.rb:1:in `require'
from narray.rb:1

Why is that? Why cannot I get it running as throigh irb?

Of I insert

require "rubygems", I get the following:


C:\Users\Abder-Rahman\Desktop\Research>ruby narray.rb
/narray.rb:3: uninitialized constant NArray (NameError)
from
C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
from
C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
from narray.rb:2
 
B

Brian Candler

Abder-Rahman Ali said:
Thanks Brian.

I have renamed the .rb file as: nary.rb

When I run the script, I get the following:

C:\Users\Abder-Rahman\Desktop\Research>ruby nary.rb
nary.rb:1:in `require': no such file to load -- narray (LoadError)
from nary.rb:1

Provided that I have installed the "narray" gem using:

gem install narray

If you're running ruby 1.8.x, you'll need

require "rubygems" # << THIS IS MISSING
require "narray"
And, just a small thing. When you said: "Don't call your own script
'narray.rb', because then Ruby will think it has already loaded the
narray library."

Can you just clarify this point?

Ruby keeps tracks of which libraries it has already loaded - actually in
a variable called $LOADED_FEATURES - so that if you require the same
library a second time, it doesn't get re-loaded.

So when you do

require 'narray'

it will be skipped if narray.rb has already been loaded.

Unfortunately, if your script is called narray.rb, and you run it from
the command line, then narray.rb *has* already been loaded, so line 1 of
your script is skipped. It then continues to line 2, where it tries to
use NArray and finds it undefined.

If you rename your script to nary.rb, then line 1 tries to find a file
called narray.rb, and gives you a different error. You fix it by
installing narray.rb somewhere in the library path (e.g. by installing
the narray gem, and doing require 'rubygems')
 
A

Abder-Rahman Ali

@Brian. Thanks a lot. You are correct.

It runs now without errors, but the remaining case is, how can I display
the result (Array) as the one that appeared when running the program
using "irb"?
 
A

Abder-Rahman Ali

It displayed the result when I typed the following in the script:

p nary1

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top