+ 2

Calling the new method.

ok the syntax for calling the new method is- classname.method name now see this- Class Dog def bark puts "woof" end end d=Dog.new d.bark In the last line we are calling the new method and d is the objects name. which is objectsname.methodname which is contradicting to classname.methodname. which one is the right syntax. plis help thanx

18th Mar 2017, 6:31 PM
stephen haokip
stephen haokip - avatar
2 Answers
0
Hi Stephen, check the comments here maybe it's easier to understand: class Dog # defining a new class (or object) called Dog def bark # defining a new method for the class (or object) Dog called bark puts "Woof!" end end d = Dog.new # here we are saying that d is equal to Dog =begin So if d is now equal to Dog it will have exactly the same methods inside and that's the reason why we can call d.bark becaus is exactly the same result if will call Dog.bark. The real difference is that we cannot call Dog.bark otherwise Ruby will complain becuase we created d as Dog but we don't have any live Dog class You have to imagine that the class (or object) Dog doesn't exists untill you will create it by associating it to a variable that will heredits its proprieties included the methods =end d.bark # here we call the method bark that is inside d that is a Dog Class
8th Apr 2017, 3:34 PM
Claudio Proietti
Claudio Proietti - avatar
- 1
you must distinguish instance method and class method first...
19th Mar 2017, 3:13 PM
Steven Wang
Steven Wang - avatar