ブロックを使って、ソースコードを一カ所にまとめる。
class Bike def start puts "start" end def stop puts "stop" end end悪いコード
class BigBike def start begin bike = Bike.new bike.start rescue Exception => e puts "failed: #{e}" end end def stop begin bike = Bike.new bike.stop rescue Exception => e puts "failed: #{e}" end end end良いコード
class BigBike def start big_bike do |v| v.start end end def stop big_bike do |v| v.stop end end def big_bike begin bike = Bike.new yield bike rescue Exception => e puts "failed: #{e}" end end
0 件のコメント:
コメントを投稿