Why is the type
variable available in the is_a_peacock?
method in the following?
class Animal
attr_reader :type
def initialize(type)
@type = type
end
def is_a_peacock?
if type == "peacock"
return true
else
return false
end
end
end
an = Animal.new('peacock')
puts an.is_a_peacock? # => true
Why does commenting out the initialization of @type
make type
unavailable?
class Animal
attr_reader :type
def initialize(type)
#@type = type
end
def is_a_peacock?
if type == "peacock"
return true
else
return false
end
end
end
an = Animal.new('peacock')
puts an.is_a_peacock? # => false
"Why does commenting out the initialization of
@type
maketype
unavailable?"
It does not. Method type
is available. Just not initialized. Because you commented that out.
If method type
wasn't available, your program would crash. (As it does when you comment out the attr_reader
).