Module 03 Β· Classes & Modules β Lesson 2 of 6 Β· ~9 min
attr_accessor & Friends
First, the manual fix β so it's not magic
Last lesson ended with priya.name raising NoMethodError. The direct fix is a method that just hands back the ivar:
class Walker
def initialize(name)
@name = name
end
def name
@name
end
endThat's it β name is a completely ordinary method whose body happens to be a single ivar. Now priya.name calls that method and gets "Priya" back. Nothing hidden: a "getter" in Ruby is just a method, same shape as any other.