我在使用我的程序正確檢查Exterior Finish(ext)和之前創建的對象實例之間的等效性時遇到了一些問題。是instance_of?這裏的問題,因爲我沒有正確使用它?檢查變量和對象之間的等效性
##################################################
class Wood
end
class Brick
end
class Other
end
#################################################
asbestos = Wood.new #'Wood'
cement_board = Wood.new #'Wood'
frame_clapboard = Wood.new #'Wood'
vinyl = Wood.new #'Wood'
asphalt = Wood.new #'Wood'
wood_shake = Wood.new #'Wood'
brick_stone = Brick.new #'Brick'
brick_stone_veneer = Brick.new #'Brick'
concrete = Brick.new #'Brick'
glass = Other.new #Other Not Supported
other = Other.new #Other Types Not Supported
stucco = Other.new #Not Suppored
aluminum = Other.new #Not supported
#################################################
puts "Enter name of Exterior Finish: "
ext = gets.chomp
puts"Enter year of building completion: "
year = gets.chomp
if ext.instance_of? Wood
puts "Tis Wood\n"
if year.to_i.between?(1850, 1942)
print "pre war wood\n"
elsif year.to_i.between?(1943, 1977)
print "post war wood\n"
elsif year.to_i.between?(1978, 2005)
print "near present wood\n"
elsif year.to_i.between?(2006, 2014)
print "present wood\n"
end
elsif ext.instance_of? Brick
puts "Tis Brick\n"
if year.to_i.between?(1850, 1942)
print "pre war brick\n"
elsif year.to_i.between?(1943, 1977)
print "post war brick\n"
elsif year.to_i.between?(1978, 2005)
print "near present brick\n"
elsif year.to_i.between?(2006, 2014)
print "present brick\n"
end
else
print "Of Type Not Supported\n"
end
您從控制檯得到'ext'。這是一個字符串。當然,它不是上面定義的類中的一個。 – 2014-11-25 07:11:25
你可能需要像'if if =='Wood''那樣的東西。 – 2014-11-25 07:15:43
@SergioTulentsev分機應該是我比較類中的對象的字符串。在僞代碼中:如果在類Wood中是ext == x。 X是類Wood中的實例或對象之一,就像我上面代碼中的石棉一樣。 – 2014-11-25 07:16:05