2014-11-25 27 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

相关问题