Created
November 23, 2010 15:30
-
-
Save yoggy/711936 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
#!/usr/bin/ruby | |
# | |
# Rubyでブロックを引数に受ける方法&ブロックを引数として渡す方法 | |
# | |
class ArrayProxy | |
def initialize | |
@ary = [] | |
end | |
def <<(v) | |
@ary << v | |
end | |
# ここがポイント | |
# 変数に&をつけると、ブロックを受け取ることができる | |
def each(&block) | |
# &をつけてeachへブロックを渡す | |
@ary.each(&block) | |
end | |
end | |
# | |
ap = ArrayProxy.new | |
ap << 1 | |
ap << 2 | |
ap << 3 | |
ap.each {|n| | |
puts n | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment