Created
October 19, 2008 13:21
-
-
Save yhara/17824 to your computer and use it in GitHub Desktop.
「世界のナベアツ問題」をRubyで解いてみました。Unicodeで保存し、 ruby -Ku nabeatsu-run.rb で実行してください
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require 'nabeatsu' | |
include Nabeatsu | |
(1.から 40).まで{ | |
3.の倍数 と 3.がつく数字 の時だけ アホに なって | |
5.の倍数 の時だけ 犬っぽく なって | |
8.の倍数 の時だけ 気持ちよく なります | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
class Integer | |
def から(まで) | |
Range.new(self, まで) | |
end | |
def の倍数(info) | |
n, how = *info | |
if n % self == 0 | |
throw :hit, how[n] | |
end | |
end | |
def がつく数字(info) | |
n, how = *info | |
if n.to_s.include?(self.to_s) | |
throw :hit, how[n] | |
else | |
how | |
end | |
end | |
end | |
class Range | |
def まで | |
self.each{|n| | |
x = catch(:hit){ | |
Nabeatsu.数!(n) | |
yield | |
} | |
puts (x ? x : n) | |
} | |
end | |
end | |
module Nabeatsu | |
def self.数 | |
@@数 | |
end | |
def self.数!(新しい数) | |
@@数 = 新しい数 | |
end | |
def の時だけ(how) | |
[Nabeatsu.数, how] | |
end | |
alias と の時だけ | |
def アホに(_) | |
lambda{|n| "(アホな #{n})"} | |
end | |
def 犬っぽく(_) | |
lambda{|n| "(犬っぽい #{n})"} | |
end | |
def 気持ちよく(_) | |
lambda{|n| "(気持ちいい #{n})"} | |
end | |
def なって; end | |
def なります; end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment