{"id":483,"date":"2005-10-26T07:00:00","date_gmt":"2005-10-25T22:00:00","guid":{"rendered":"https:\/\/ecoop.net\/memo\/2005-10-26-7.html"},"modified":"2005-10-26T07:00:00","modified_gmt":"2005-10-25T22:00:00","slug":"2005-10-26-7","status":"publish","type":"post","link":"https:\/\/www.ecoop.net\/memo\/archives\/2005-10-26-7.html","title":{"rendered":"yield"},"content":{"rendered":"<p>\n&#8211; \u51e6\u7406\u3092\u5834\u5408\u306b\u5fdc\u3058\u3066\u81ea\u7531\u306b\u5909\u3048\u3089\u308c\u308b\u3002\u30b9\u30de\u30fc\u30c8\u306b\u306a\u3063\u305f Visitor \u30d1\u30bf\u30fc\u30f3\u307f\u305f\u3044\u306a\u3082\u306e\u304b\u306a\u3041..<br \/>\n\u30a4\u30c6\u30ec\u30fc\u30b7\u30e7\u30f3\u3092 Iterator \u30d1\u30bf\u30fc\u30f3\u306a\u3057\u3067\u3067\u304d\u308b(\u5185\u90e8\u3067 Iterator \u30d1\u30bf\u30fc\u30f3\u3092\u4f7f\u3063\u3066\u5916\u306b\u306f each \u30e1\u30bd\u30c3\u30c9\u3092\u516c\u958b\u3059\u308b\u7b49)\u3002\n<\/p>\n<blockquote><p>class Iterator<br \/>\n&nbsp;&nbsp;@values<br \/>\n&nbsp;&nbsp;@nIndex<br \/>\n&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;def initialize(*values)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;@values = values.dup<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;@nIndex = 0<br \/>\n&nbsp;&nbsp;end<br \/>\n&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;def each<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;yield(getNext) while hasNext<br \/>\n&nbsp;&nbsp;end<br \/>\n&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;def hasNext<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return @nIndex < @values.size<br \/>\n&nbsp;&nbsp;end<br \/>\n&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;private :hasNext<br \/>\n&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;def getNext<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;throw RangeError.new(&#8220;no element for next&#8221;) if !hasNext<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;res = @values[@nIndex]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;@nIndex += 1<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return res<br \/>\n&nbsp;&nbsp;end<br \/>\n&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;private :getNext<br \/>\nend<br \/>\n&nbsp;<br \/>\niterator = Iterator.new(0,1,2,3,4)<br \/>\niterator.each{ |i|<br \/>\n&nbsp;&nbsp;print( i * 2, &#8220;, &#8221; )<br \/>\n}<br \/>\n# result:<br \/>\n# 0, 2, 4, 6, 8, <br \/>\n# \n<\/p><\/blockquote>\n<p>\u3053\u306e\u4f8b\u306f\u305d\u306e\u307e\u307e [0,1,2,3,4].each{|i| &#8230; } \u3068\u3057\u305f\u6642\u306e\u52d5\u4f5c\u3068\u540c\u3058\u3060\u3051\u3069\u3001\u30de\u30b9\u30bf\u30fc\u30c7\u30fc\u30bf\u304c\u81a8\u5927\u306b\u306a\u308a\u3046\u308b\u6642\u306a\u3069\u306f\u81ea\u5206\u3067 yield \u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3059\u308b\u3068\u30b3\u30b9\u30c8\u524a\u6e1b\u306b\u306a\u3063\u3066\u3088\u3044\u3002<br \/>\n&nbsp;<br \/>\n\u53c2\u8003:<br \/>\n&lt;<a href=\"http:\/\/www.ruby-lang.org\/ja\/man\/?cmd=view;name=%A5%E1%A5%BD%A5%C3%A5%C9%B8%C6%A4%D3%BD%D0%A4%B7#yield\">http:\/\/www.ruby-lang.org\/ja\/man\/?cmd=view;name=%A5%E1%A5%BD%A5%C3%A5<br \/>\n%C9%B8%C6%A4%D3%BD%D0%A4%B7#yield<\/a>><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8211; \u51e6\u7406\u3092\u5834\u5408\u306b\u5fdc\u3058\u3066\u81ea\u7531\u306b\u5909\u3048\u3089\u308c\u308b\u3002\u30b9\u30de\u30fc\u30c8\u306b\u306a\u3063\u305f Visitor \u30d1\u30bf\u30fc\u30f3\u307f\u305f\u3044\u306a\u3082\u306e\u304b\u306a\u3041.. \u30a4\u30c6\u30ec\u30fc\u30b7\u30e7\u30f3\u3092 Iterator \u30d1\u30bf\u30fc\u30f3\u306a\u3057\u3067\u3067\u304d\u308b(\u5185\u90e8\u3067 Iterator \u30d1\u30bf\u30fc\u30f3\u3092\u4f7f\u3063\u3066\u5916 &hellip; <a href=\"https:\/\/www.ecoop.net\/memo\/archives\/2005-10-26-7.html\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">yield<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[15],"class_list":["post-483","post","type-post","status-publish","format-standard","hentry","tag-ruby"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/comments?post=483"}],"version-history":[{"count":0,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/483\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/media?parent=483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/categories?post=483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/tags?post=483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}