Skip to content

Instantly share code, notes, and snippets.

@yoggy
Created November 23, 2010 15:30
Show Gist options
  • Save yoggy/711936 to your computer and use it in GitHub Desktop.
Save yoggy/711936 to your computer and use it in GitHub Desktop.
#!/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