Last active
August 27, 2016 00:45
-
-
Save kenzotakahashi/4f829114c44fc40afc11f12bb04081d1 to your computer and use it in GitHub Desktop.
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
yamanote = ["大崎","五反田","目黒","恵比寿","渋谷","原宿","代々木","新宿","新大久保","高田馬場","目白","池袋","大塚","巣鴨","駒込","田端","西日暮里","日暮里","鶯谷","上野","御徒町","秋葉原","神田","東京","有楽町","新橋","浜松町","田町","品川"] | |
class ArrayIterator | |
def initialize(array) | |
@array = array | |
@index = 0 | |
end | |
def item | |
@array[@index] | |
end | |
def soto_mawari | |
value = @array[@index] | |
@index = (@index == @array.length - 1) ? 0 : @index + 1 | |
value | |
end | |
def uchi_mawari | |
value = @array[@index] | |
@index = (@index == 0) ? @array.length - 1 : @index - 1 | |
value | |
end | |
end | |
iter = ArrayIterator.new(yamanote) | |
# (0..30).each do |_| | |
# puts "#{iter.soto_mawari}" | |
# end | |
(0..30).each do |_| | |
puts "#{iter.uchi_mawari}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment