2014-11-25 61 views
0

我在使用我的程序正確檢查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 
+3

您從控制檯得到'ext'。這是一個字符串。當然,它不是上面定義的類中的一個。 – 2014-11-25 07:11:25

+0

你可能需要像'if if =='Wood''那樣的東西。 – 2014-11-25 07:15:43

+0

@SergioTulentsev分機應該是我比較類中的對象的字符串。在僞代碼中:如果在類Wood中是ext == x。 X是類Wood中的實例或對象之一,就像我上面代碼中的石棉一樣。 – 2014-11-25 07:16:05

回答

0
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 
ext=eval (ext) rescue nil # this will convert entered text to corresponding class or instance or if user enter invalid text then it will set ext = nil 

puts"Enter year of building completion: " 
year = gets.chomp 

if ext.is_a? 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.is_a? 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 
+1

只有在輸入木材或磚塊時纔有效,但打算使用石棉或鋁粉或灰泥作爲輸入,然後檢查是否實際上是木材或磚塊。 – 2014-11-25 13:23:29

+0

它現在已經更新,它會接受任何聲明的實例 – 2014-11-27 09:47:34