Skip to content

Instantly share code, notes, and snippets.

@shime
Created April 16, 2012 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shime/2397386 to your computer and use it in GitHub Desktop.
Save shime/2397386 to your computer and use it in GitHub Desktop.
writting string to file
# downloading content from the web
require 'open-uri'
url = 'http://blog.stackoverflow.com/audio/stackoverflow-podcast-001.mp3'
open(url).read.to_file(url.split('/').last)
# And boom, you’ve downloaded the file to “stackoverflow-podcast-001.mp3”
class String
def to_file(filename)
File.open(filename, 'w') {|f| f.write self }
end
end
"Hello file!".to_file("hello.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment