Skip to content

Instantly share code, notes, and snippets.

@skatenerd
Created March 4, 2020 20:50
Show Gist options
  • Save skatenerd/396a3e379a6068f3c294dd878840abec to your computer and use it in GitHub Desktop.
Save skatenerd/396a3e379a6068f3c294dd878840abec to your computer and use it in GitHub Desktop.
ruby gotcha
# 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