Created
June 13, 2012 15:30
-
-
Save kimihito/2924772 to your computer and use it in GitHub Desktop.
たのしいRuby10章
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 -*- | |
def cels2fahr(cels) | |
fahr = cels * 9 / 5 + 32 | |
p fahr | |
end | |
cels2fahr(10) #=> 50 | |
cels2fahr(20) #=> 68 |
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
#!/usr/bin/env ruby | |
#-*- coding: utf-8 -*- | |
def dice() | |
p rand(6) | |
end | |
dice() |
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
#!/usr/bin/env ruby | |
#-*- coding: utf-8 -*- | |
def dice | |
rand(6) | |
end | |
def dice10 | |
sum = 0 | |
10.times do | |
sum += rand(6) | |
end | |
sum | |
end | |
p dice10 |
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
#!/usr/bin/env ruby | |
#-*- coding: utf-8 -*- | |
def fahr2cels(fa) | |
cels =(5.0 / 9.0) * (fa - 32.0) | |
end | |
i = 1 | |
while i <= 100 do | |
print"華氏#{i}度:" | |
print"摂氏#{fahr2cels(i)}\n" | |
i += 1 | |
end | |
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
#!/usr/bin/env ruby | |
#-*- coding: utf-8 -*- | |
def div(n) | |
(1..n).select{|x| n % x == 0} | |
end | |
def prime?(n) | |
div(n) == [1,n] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment