Skip to content

Instantly share code, notes, and snippets.

@yancya
Created April 22, 2024 08:13
Show Gist options
  • Save yancya/872500c98dbea057a1cb0826c0f1d4cb to your computer and use it in GitHub Desktop.
Save yancya/872500c98dbea057a1cb0826c0f1d4cb to your computer and use it in GitHub Desktop.
Python触ってて出てきたwithがなんなのか分からなかったので調べた
from contextlib import contextmanager
@contextmanager
def hoge():
try:
print("start")
yield "Hello, "
finally:
print("end")
with hoge() as msg:
print(msg + "World!") #=> Hello, World!
def hoge
puts("start")
yield "Hello, "
ensure
puts("end")
end
hoge do |msg|
puts(msg + "World!") #=> Hello, World!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment