How to download image files in #ruby
▻https://hackernoon.com/how-to-download-image-files-in-ruby-c189ba52fad6?source=rss----3a8144eab
Have you ever needed to download and save an image in your Ruby application? Read on to find out how.Plain old RubyThe most popular way to download a file without any dependencies is to use the standard library open-uri.Kernel#open is a method that you can use to open files, streams, or processes to read to or write from. For example, you can open a file and read its contents with the following code:open("./test.txt") do |file| puts file.readendopen-uri extends Kernel#open so that it can open URIs as if they were files. We can use this to download an image and then save it as a file.To do so, we first require open-uri then use the open method to access an image URL. We can then open up a file and write the contents of the image to the file. Open up IRB and try the (...)
#ruby-download #ruby-development #images-in-ruby #ruby-on-rails