[파일명:  testStringFindInList.rb]------------------------------------------------
def find(arr, s)
    for i in 0..arr.size() do
     if arr[i].index(s) != nil
      return i
        end
    end
    return -1;
end

def printArray(arr)
    print("[")
    for i in 0..arr.size() - 1 do
         print(arr[i] + ", ")
    end
    if arr.size() > 0
        print(arr[arr.size() - 1])
    end
    print("]\n")
end

words = ["하나", "둘", "셋", "넷", "다섯", "여섯"]

print("list: ")
printArray(words)
where = find(words, "셋")
if where > 0
    print("발견!  ")
    print("Next word of 셋 in list: " + words[where+1] + "\n")
end

print("Sorting..." + "\n")
words = words.sort()

print("list: ")
printArray(words)
where = find(words, "셋")
if where > 0
    print("발견!  ")
    print("Next word of 셋 in list: " + words[where+1] + "\n")
end
------------------------------------------------


실행> ruby testStringFindInList.rb
list: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in list: 넷
Sorting...
list: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in list: 여섯



크리에이티브 커먼즈 라이선스
Creative Commons License

Posted by Scripter
,