I'm perplexed as to why I need to selectively refer to an instance variable with "self" inside a case statement inside a class method:
I have a class with an instance method @working_dir
:
class FileSystem
attr_accessor :sizes, :working_dir
attr_reader :input
def initialize(input)
@input = input.split("\n")
@sizes = Hash.new(0)
@working_dir = []
end
...
end
I've defined a method parse_cmd
that performs an operation on @working_dir
depending on the outcome of a case statement:
...
def parse_cmd(str)
cmd_arr = str.split(' ')
return unless cmd_arr[1] == 'cd'
case cmd_arr[2]
when '..'
working_dir.pop
when '/'
self.working_dir = ['/']
else
working_dir << cmd_arr[2]
end
end
...
Rubocop/the interpreter yells at me if I exclude the self
on self.working_dir = ['/']
. Why is this? Why do I need to include it here, but not on other references to @working_dir
within the case statement?