Last active
January 7, 2016 00:24
-
-
Save frsyuki/f93db28d446c3566f39e to your computer and use it in GitHub Desktop.
This file contains 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
# re: http://twitter.com/kazuho/status/684889103796600832 | |
def run_each(obj) | |
obj.each {|v| p v } | |
obj.close | |
end | |
def test | |
o = Struct.new(:is_open) do | |
def each(&block) | |
block.yield "hello\n" | |
end | |
def close | |
self.is_open = false | |
end | |
end.new | |
run_each(o) | |
if o.is_open | |
raise "close is not called!" | |
end | |
end | |
test |
This file contains 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
# re: http://twitter.com/kazuho/status/684889103796600832 | |
def run_each(obj) | |
obj.each {|v| p v } | |
obj.close | |
end | |
def test | |
o = Class.new do | |
def initialize | |
@is_open = false | |
end | |
attr_reader :is_open | |
def each(&block) | |
block.yield "hello\n" | |
end | |
def close | |
@is_open = false | |
end | |
end.new | |
run_each(o) | |
if o.is_open | |
raise "close is not called!" | |
end | |
end | |
test |
This file contains 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
# re: http://twitter.com/kazuho/status/684889103796600832 | |
def run_each(obj) | |
obj.each {|v| p v } | |
obj.close | |
end | |
def test | |
is_open = true | |
c = Class.new do | |
define_method(:each) {|&block| block.yield "hello\n" } | |
define_method(:close) { is_open = false } | |
end | |
run_each(c.new) | |
if is_open | |
raise "close is not called!" | |
end | |
end | |
test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment