0

Why we use same name of instance variable as the argument name( of initialize method) followed by @

2nd Dec 2016, 12:57 PM
abhishek sharma
abhishek sharma - avatar
2 Réponses
+ 2
Not necessary that variable name and argument name be same. We do that just for our convenience/understanding. Try the code below: class Person def initialize(name) @pname = name end def get_name @pname end end p = Person.new("David") puts p.get_name It is only important for one to use that same variable @pname for other functions (like in get_name). Argument names don't bear any significance as such. It is just for our understanding, or to make the code look more simple, sensible, and self-explanatory.
2nd Dec 2016, 5:55 PM
PythonProMaybe
+ 1
coz whatever paramater you passed to your class. has to be passed to the instance variable also. and instance variable is preceded by the sign@
13th Feb 2017, 8:20 PM
stephen haokip
stephen haokip - avatar