Skip to content

Instantly share code, notes, and snippets.

@babie
Last active December 21, 2015 05:39
Show Gist options
  • Save babie/6258857 to your computer and use it in GitHub Desktop.
Save babie/6258857 to your computer and use it in GitHub Desktop.
require Ruby 2.0.0 or newer
#!/usr/bin/env ruby
module Temperature
refine Numeric do
def to_fahr
self.to_f * 9 / 5 + 32
end
def to_cels
(self.to_f - 32) * 5 / 9
end
end
end
if __FILE__ == $0
using Temperature
modes = {
1 => "華氏(F)から摂氏(C)",
2 => "摂氏(C)から華氏(F)",
}
modes.each do |key, val|
puts "#{key} #{val}"
end
print "選択してください: "
case gets.to_i
when 1
print "温度(F)を入力してください: "
f = gets.to_f
puts "#{f}F = #{f.to_cels.round(1)}C"
when 2
print "温度(C)を入力してください: "
c = gets.to_f
puts "#{c}C = #{c.to_fahr.round(1)}F"
else
puts "不正な入力です。"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment