0
Why we use same name of instance variable as the argument name( of initialize method) followed by @
2 Answers
+ 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.
+ 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@