=> e # Τϥʔ͕ى͖ͨ߹ͷॲཧ puts "Τϥʔ͕ൃੜ͠·ͨ͠: #{e.message}” end # ϝιουશମ͕ྫ֎ॲཧͷରʹͳΔ߹begin/endΛলུͰ͖Δ def some_method 1 / 0 rescue ZeroDivisionError => e puts "ZeroDivisionError͕ൃੜ͠·ͨ͠: #{e.message}" end # ্ͷίʔυͱԼͷίʔυಉ͡ def some_method begin 1 / 0 rescue ZeroDivisionError => e puts "ZeroDivisionError͕ൃੜ͠·ͨ͠: #{e.message}" end end
ԿΒ͔ͷΤϥʔॲཧ end # ྫ֎ΫϥεΛࢦఆ͠ͳ͚ΕStandardErrorͱͦͷαϒΫϥε͕rescue͞ΕΔ begin save(user) rescue => e # ԿΒ͔ͷΤϥʔॲཧ end • ্ͷ2͍ͭͬͯΔ͜ͱಉ͡ • ͳͷͰɺԼͷΑ͏ʹStandardErrorলུ͢Εྑ͍
heisei_text.match(/ฏ(?<jp_year>\d+)(?<month>\d+)݄(?<day>\d+)/) year = m[:jp_year].to_i + 1988 month = m[:month].to_i day = m[:day].to_i # ྫ֎ॲཧͷൣғΛڱΊɺัଊ͢Δྫ֎ΫϥεΛݶఆ͢Δ begin Date.new(year, month, day) rescue ArgumentError # ແޮͳͰ͋ΕnilΛฦ͢ nil end end
m[:jp_year].to_i + 1988 month = m[:month].to_i day = m[:day].to_i # ྫ֎ॲཧͰͳ͘ɺ݅ذΛ͏ if Date.valid_date?(year, month, day) Date.new(year, month, day) end end
0 rescue => e puts "Τϥʔ͕ൃੜ͠·ͨ͠: #{e.message}" puts e.backtrace end class SampleTest < Minitest::Test def test_some_method assert_output /Τϥʔ͕ൃੜ͠·ͨ͠/ do some_method end end end
currency_of(country) case country when :japan 'yen' when :us 'dollar' when :india 'rupee' else raise ArgumentError, "ແޮͳࠃ໊Ͱ͢ɻ#{country}" end end # ྫ֎͕ൃੜ͢Δ currency_of(:italy) #=> ArgumentError: ແޮͳࠃ໊Ͱ͢ɻitaly
case country when :japan 'yen' when :us 'dollar' when :india 'rupee' end end # ఆ֎ͷࠃ໊Λ͢ͱnil͕ฦΔ currency = currency_of(:italy) #=> nil # ༧ظͤ͵λΠϛϯάͰΤϥʔ͕ൃੜ͢Δ currency.upcase #=> NoMethodError: undefined method `upcase' for nil:NilClass
case country when :japan 'yen' when :us 'dollar' else 'rupee' end end # ໃ६͕ͨ͠ฦ͖ͬͯͯ͠·͏ country = :italy currency = currency_of(country) #=> “rupee” price = 100 "#{country}: #{price}#{currency}" #=> "italy: 100rupee"
extend_deadline_for_all_stakeholders(project, new_deadline) # ສҰͷࣄଶʹඋ͓͑ͯ͘ʢσʔλϕʔεͷมߋΛΞτϛοΫૢ࡞ʹ͢Δʣ transaction do # ୭Ұਓͱͯ͠కΊΓͷԆʹจ۟ΛݴΘͳ͍ͣͩʂʢจ۟ΛݴΘΕͨΒγεςϜΤϥʔʣ project.developers.each do |developer| developer.update!(deadline: new_deadline) end project.customer.update!(deadline: new_deadline) project.manager.update!(deadline: new_deadline) end end