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
It does not. Method
typeis available. Just not initialized. Because you commented that out.If method
typewasn't available, your program would crash. (As it does when you comment out theattr_reader).