7stud2
(7stud --)
1
I'm learning about method calling and object oriented programming in
Ruby. The book I'm reading gives an example about calling a method.
I have two methods:
a mainMethod and DOG class.
My mainMethod reads as follows:
require_relative 'dog'
class MAINMETHOD
DOG.bark
end
My DOG class reads as follows:
class DOG
def bark
puts "woof"
end
end
Whenever I try to run mainMethod, I get "undefined method `bark' for
DOG:Class (NoMethodError)"
Am I doing something wrong? Please help
···
--
Posted via http://www.ruby-forum.com/.
Chris6
(Chris)
2
the way bark is defined it is an instance method
you need to create an instance of the DOG class to call it:
DOG.new.bark
or you change the definition of bark to be a class method:
def self.bark
...
which then can be called using the class
have a look at
more details
···
On Fri, Jan 17, 2014 at 8:39 PM, Slipper Spe <lists@ruby-forum.com> wrote:
I'm learning about method calling and object oriented programming in
Ruby. The book I'm reading gives an example about calling a method.
I have two methods:
a mainMethod and DOG class.
My mainMethod reads as follows:
require_relative 'dog'
class MAINMETHOD
DOG.bark
end
My DOG class reads as follows:
class DOG
def bark
puts "woof"
end
end
Whenever I try to run mainMethod, I get "undefined method `bark' for
DOG:Class (NoMethodError)"
Am I doing something wrong? Please help
--
Posted via http://www.ruby-forum.com/\.
7stud2
(7stud --)
3
Figured it out, can't believe I didn't think of it earlier-- I
dishonored my Java knowledge.
I completely forgot to call dog:
dogCaller = DOG.new
dogCaller.bark
Got caught up with learning Ruby and forgot my older principles.
···
--
Posted via http://www.ruby-forum.com/.
7stud2
(7stud --)
4
Please don't UPCASE all your classes man ...
···
--
Posted via http://www.ruby-forum.com/.