Last active
December 14, 2018 15:01
-
-
Save hanachin/8636e443e8bb459281ad210255f1fe15 to your computer and use it in GitHub Desktop.
配列にためる手間なしenum_for ref: https://qiita.com/hanachin_/items/134000befc7f5fdce94c
This file contains hidden or 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
def 動物API | |
@動物API ||= Object.new | |
end | |
def 動物API.page(page) | |
{ | |
1 => ["鶏"], | |
2 => ["豚"], | |
3 => ["牛"] | |
}[page] || [] | |
end | |
def いろんな肉 | |
page = 1 | |
肉 = [] | |
while true | |
animals = 動物API.page(page) | |
break if animals.empty? | |
肉 += animals | |
page += 1 | |
end | |
肉 | |
end | |
いろんな肉.each do |肉| | |
puts 肉 | |
end | |
# 鶏 | |
# 豚 | |
# 牛 |
This file contains hidden or 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
def 動物API | |
@動物API ||= Object.new | |
end | |
def 動物API.page(page) | |
{ | |
1 => ["鶏"], | |
2 => ["豚"], | |
3 => ["牛"] | |
}[page] || [] | |
end | |
def each_いろんな肉 | |
page = 1 | |
while true | |
animals = 動物API.page(page) | |
break if animals.empty? | |
animals.each {|a| yield a } | |
page += 1 | |
end | |
end | |
each_いろんな肉 do |肉| | |
puts 肉 | |
end | |
# 鶏 | |
# 豚 | |
# 牛 |
This file contains hidden or 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
def 動物API | |
@動物API ||= Object.new | |
end | |
def 動物API.page(page) | |
{ | |
1 => ["鶏"], | |
2 => ["豚"], | |
3 => ["牛"] | |
}[page] || [] | |
end | |
def each_いろんな肉 | |
page = 1 | |
while true | |
animals = 動物API.page(page) | |
break if animals.empty? | |
animals.each {|a| yield a } | |
page += 1 | |
end | |
end | |
def いろんな肉 | |
enum_for(:each_いろんな肉).to_a | |
end | |
puts いろんな肉.join("と") + "を全部焼いたもの" | |
# 鶏と豚と牛を全部焼いたもの |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment