Uninitialized constant NArray (Name Error)

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.

Abder-Rahman A. wrote:

Following section (2) here:
Dennou-Ruby Tutorial

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/

Looks like you need to install the library

install from source (requires compiler)

gem install narray

Windows binary (MinGW version; works also with VC6 mswin32)

gem install narray --platform=x86-mingw32

From here http://narray.rubyforge.org/

PRELOAD:
irb(main):001:0> require ‘narray’
LoadError: no such file to load – narray
from (irb):1:in `require’
from (irb):1

INSTALL:
! gem install narray
Password:
Building native extensions. This could take a while…
Successfully installed narray-0.5.9.7
1 gem installed
Installing RDoc documentation for narray-0.5.9.7…

Usage:
irb(main):004:0> require “rubygems”
=> true
irb(main):005:0> require “narray”
=> true
irb(main):006:0> ary1 = NArray.sfloat(3,4)
=> NArray.sfloat(3,4):
[ [ 0.0, 0.0, 0.0 ],
[ 0.0, 0.0, 0.0 ],
[ 0.0, 0.0, 0.0 ],
[ 0.0, 0.0, 0.0 ] ]
irb(main):007:0>


From: Abder-Rahman A. [email protected]
Reply-To: [email protected]
Date: Thu, 2 Sep 2010 10:09:33 -0500
To: ruby-talk ML [email protected]
Subject: uninitialized constant NArray (Name Error)

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.

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.

@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:inrequire’
from narray.rb:2

Abder-Rahman A. wrote:

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’)

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.

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

p nary1

Thanks.

@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”?

Ali,

YW! :slight_smile:

In the end I agree with Brian, you should not call it narry.rb

Cheers,
j


From: Abder-Rahman A. [email protected]
Reply-To: [email protected]
Date: Thu, 2 Sep 2010 10:48:50 -0500
To: ruby-talk ML [email protected]
Subject: Re: uninitialized constant NArray (Name Error)

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.

Thanks @Savard.

Again, I think Brian explained this and quite accurately.

I named my file gonary.rb

contents
require “rubygems”
require “narray”

ary1 = NArray.sfloat(3,4)
puts ary1.inspect

Results

MANISH.local:jes [09-03 07:03] 0 503:3 (0.16 Mb) ? ~/dev/noodle
! ruby gonary.rb
NArray.sfloat(3,4):
[ [ 0.0, 0.0, 0.0 ],
[ 0.0, 0.0, 0.0 ],
[ 0.0, 0.0, 0.0 ],
[ 0.0, 0.0, 0.0 ] ]

See??

Cheers mate,
j


From: Abder-Rahman A. [email protected]
Reply-To: [email protected]
Date: Thu, 2 Sep 2010 10:54:30 -0500
To: ruby-talk ML [email protected]
Subject: Re: uninitialized constant NArray (Name Error)

@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