Created
March 4, 2020 20:50
-
-
Save skatenerd/396a3e379a6068f3c294dd878840abec to your computer and use it in GitHub Desktop.
ruby gotcha
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
# We all know about this Python gotcha: | |
# def foo(x=[]): | |
# x.append(1) | |
# return len(x) | |
# But the answer is not to do what ruby does. | |
# Would you expect this to run quickly or slowly? | |
def runonce | |
urls = ["yahoo.com", "google.com", "bing.com", "marxists.org"] | |
urls.map { |url| | |
contents = `curl -L #{url}` | |
contents.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').split("\n").sample | |
} | |
end | |
def foo(lines = runonce()) | |
lines.sample | |
end | |
foo() | |
foo() | |
foo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment