Hi. I'm sure it's been asked before, but I can't find the specific
answer. Basically I am doing the following..........
1.Created a ruby file Person.rb
class Person
def initialize(name,age)
@name = name
@age = age
end
def name
return @name
end
def age
return @age
end
end
2. I then run it
ruby Person.rb
3. Then when I try to instansiate a person object........
(in irb mode) p1 = Person.new("John",30)
I get the following error......
NameError: uninitialized constant Object::Person
I don't understand what I'm missing.
Thanks
···
--
Posted via http://www.ruby-forum.com/ .
Step 2 won't make your script available to irb. So until your script
does something else, step 2 won't do anything.
Step 3, in irb you need to:
require 'person'
p = Person.new('John', 30)
···
On Wed, Oct 5, 2011 at 7:53 PM, Paul Roche <prpaulroche@gmail.com> wrote:
Hi. I'm sure it's been asked before, but I can't find the specific
answer. Basically I am doing the following..........
1.Created a ruby file Person.rb
class Person
def initialize(name,age)
@name = name
@age = age
end
def name
return @name
end
def age
return @age
end
end
2. I then run it
ruby Person.rb
3. Then when I try to instansiate a person object........
(in irb mode) p1 = Person.new("John",30)
I get the following error......
NameError: uninitialized constant Object::Person
I don't understand what I'm missing.
Thanks
--
Posted via http://www.ruby-forum.com/\ .
Mayank_K
(Mayank K.)
6 October 2011 08:12
3
One more thing which you should know is that you do not need to specify
return statement if it's the last statement of the method in ruby. So
instead of:
def name
return @name
end
You can write :
def name
@name
end
Mayank
and if your getter and setter is only generic you could use
attr_reader :name #for reading
attr_writer :name #for writing
attr_accessor :name #for both
···
--
Posted via http://www.ruby-forum.com/ .
Alternatively, you can invoke irb with the -r option:
$ irb -r yourlibrary
···
On Wed, Oct 5, 2011 at 7:33 PM, James Earl <james@truckhardware.ca> wrote:
Step 2 won't make your script available to irb. So until your script
does something else, step 2 won't do anything.
Step 3, in irb you need to:
require 'person'
p = Person.new('John', 30)
On Wed, Oct 5, 2011 at 7:53 PM, Paul Roche <prpaulroche@gmail.com> wrote:
Hi. I'm sure it's been asked before, but I can't find the specific
answer. Basically I am doing the following..........
1.Created a ruby file Person.rb
class Person
def initialize(name,age)
@name = name
@age = age
end
def name
return @name
end
def age
return @age
end
end
2. I then run it
ruby Person.rb
3. Then when I try to instansiate a person object........
(in irb mode) p1 = Person.new("John",30)
I get the following error......
NameError: uninitialized constant Object::Person
I don't understand what I'm missing.
Thanks
--
Posted via http://www.ruby-forum.com/\ .
--
Carina