It's just some silly codes. Not tested enough, it's not ready to handle exceptional situations, not guaranteed thread safe !!
Sometimes, I think if I have external iterator for ruby array, my world would be better. :-)
Before today, I couldn't imagine how to implement it. But I got an idea from The Pragmatic Programmer's ruby screencast #3 (Dynamic Code: blocks, bindings and define_method) this morning, then I coded it.
Although I've already heard about external iterator will be included in ruby 1.9 (right?), I believe this is good for improving my ability. :-)
#### usage example
it = [1,false,2,"abc",3,nil,4,5, :good].iterator
puts "good #{it.next}" while (it.has_next?)
#### result
good 1
good false
good 2
good abc
good 3
good
good 4
good 5
good good
Welcome any advice, corrections, suggestions.
Below lines are Korean.
어이구.. 영작 디게 힘드네요(사실 맞는지 틀리는지도 몰라요 ㅎㅎ) 그래도 예전부터 어떻게 하면 될까 고민하던거, 루비에 대한 지식이 부족해서 못하고 있다가 스크린 캐스트에어 바인딩 설명하는 부분을 보고서는 잽싸게 해결해 버렸습니다. 좋네요. Pragmatic Programmers 만세~~~!!
아 물론 외부 이터레이터가 왜 필요하냐고 반문 하실 수 도 있지만( each와 형제들이 있잖아!!), 가끔 살다보면 있으면 좋은 경우가 진짜 있다니깐요. ㅎㅎㅎㅎ
Sometimes, I think if I have external iterator for ruby array, my world would be better. :-)
Before today, I couldn't imagine how to implement it. But I got an idea from The Pragmatic Programmer's ruby screencast #3 (Dynamic Code: blocks, bindings and define_method) this morning, then I coded it.
Although I've already heard about external iterator will be included in ruby 1.9 (right?), I believe this is good for improving my ability. :-)
class Array
def iterator
index = 0
block = binding
class <<block
def has_next?
eval("index", self) <= eval("self", self).length - 1
end
def next
index = eval("index", self)
eval("index += 1", self)
eval("self", self)[index]
end
end
block
end
end
#### usage example
it = [1,false,2,"abc",3,nil,4,5, :good].iterator
puts "good #{it.next}" while (it.has_next?)
#### result
good 1
good false
good 2
good abc
good 3
good
good 4
good 5
good good
Welcome any advice, corrections, suggestions.
Below lines are Korean.
어이구.. 영작 디게 힘드네요(사실 맞는지 틀리는지도 몰라요 ㅎㅎ) 그래도 예전부터 어떻게 하면 될까 고민하던거, 루비에 대한 지식이 부족해서 못하고 있다가 스크린 캐스트에어 바인딩 설명하는 부분을 보고서는 잽싸게 해결해 버렸습니다. 좋네요. Pragmatic Programmers 만세~~~!!
아 물론 외부 이터레이터가 왜 필요하냐고 반문 하실 수 도 있지만( each와 형제들이 있잖아!!), 가끔 살다보면 있으면 좋은 경우가 진짜 있다니깐요. ㅎㅎㅎㅎ



덧글