Linking Woes

test123.rb:3: uninitialized constant Profile (NameError)

This is the error message I am getting. I have written a class Profile
which works just fine when I test it in Profile.rb. Whenever I go to
link that class in a new file, though, I get the above error.

test123's contents:

load "Profile.rb"
t = Profile.new
puts t.username

The latter two lines run perfectly well when after the Profile class
definition in Profile.rb

Any advice?

···

--
Posted via http://www.ruby-forum.com/.

Wes wrote:

test123.rb:3: uninitialized constant Profile (NameError)

This is the error message I am getting. I have written a class Profile which works just fine when I test it in Profile.rb. Whenever I go to link that class in a new file, though, I get the above error.

test123's contents:

load "Profile.rb"
t = Profile.new
puts t.username

The latter two lines run perfectly well when after the Profile class definition in Profile.rb

Any advice?

Hi Wes,

It might be easier to help you if you let us see the entire contents of both files. Also, you might try testing it out in irb to see if that sheds any more light on the issue.

-Justin

Here ya go

Profile.rb

class Profile

  attr_reader :username, :height, :weight, :age, :activity_level,
:gender

  @password #these field need never be read
  @meta_calc_strategy # The means of calculating metabolic rate

  @@male_gender = "Male"
  @@female_gender = "Female"
  @@max_activity_level = 5
  #I have decided to implement a max_activity_level as I've noticed each
  #scale for metabolic rate has its own definition of how active a
person can be.
  #When making a Strategy for calculating metabolic rate, dividing the
  #activity_level / max_activity_level will give you a percentage that
can be
  #used to place a person within any specific class.

  def initialize
    puts 1
  end

  def username= (username)
    @username = username
  end
  def password= (password)
    @password = password
  end
  def weight= (weight)
    @weight = weight
  end
  def height= (height)
    @height = height
  end
  def age= (age)
    @age = age
  end
  def activity_level= (a_l)
    @activity_level = a_l
  end
  def gender= (gender)
    @gender = gender
  end
  def meta_calc_strategy= (strategy)
    @meta_calc_strategy = strategy
  end

  def get_target_calories
    @meta_calc_strategy.get_target_calories
  end

end

test123.rb

load "Profile.rb"

t = Profile.new
puts t.username

···

--
Posted via http://www.ruby-forum.com/.

Wes wrote:

Here ya go

<snip>

I made two files, copied what you posted, and it worked fine.
Since the load command is working for you, the file is being loaded. Are you perhaps loading a different copy of the file? That's all I can think of.
You might try putting some command like

puts "I loaded!"

somewhere in the Profile.rb file (maybe at the end) to make sure it's actually being loaded.
Another thing to try is running ruby with the -d option to check for less obvious problems.

On an unrelated topic, you can replace all the methods like

  def username= (username)
    @username = username
  end

with

attr_accessor :username

Just like you used attr_reader.

Hope that helps somewhat.

-Justin