Skip to content

Instantly share code, notes, and snippets.

@covard
Last active May 19, 2022 16:28
Show Gist options
  • Save covard/84a4c8f9e6509b6e90301cacfe9e2406 to your computer and use it in GitHub Desktop.
Save covard/84a4c8f9e6509b6e90301cacfe9e2406 to your computer and use it in GitHub Desktop.
Base64 Info - file, ruby, open, modes

Base64 Info

Encode PDF and Write PDF

base64_data = Base64.encode64(File.open('/Users/covard/Documents/dummy.pdf').read)

file = File.open('/Users/covard/Desktop/test.pdf', 'wb')

file.write(Base64.decode64(base64_data))

Ruby File Open Options

Mode |  Meaning
-----+--------------------------------------------------------
"r"  |  Read-only, starts at beginning of file  (default mode).
-----+--------------------------------------------------------
"r+" |  Read-write, starts at beginning of file.
-----+--------------------------------------------------------
"w"  |  Write-only, truncates existing file
     |  to zero length or creates a new file for writing.
-----+--------------------------------------------------------
"w+" |  Read-write, truncates existing file to zero length
     |  or creates a new file for reading and writing.
-----+--------------------------------------------------------
"a"  |  Write-only, starts at end of file if file exists,
     |  otherwise creates a new file for writing.
-----+--------------------------------------------------------
"a+" |  Read-write, starts at end of file if file exists,
     |  otherwise creates a new file for reading and
     |  writing.
-----+--------------------------------------------------------
"b"  |  Binary file mode (may appear with
     |  any of the key letters listed above).
     |  Suppresses EOL <-> CRLF conversion on Windows. And
     |  sets external encoding to ASCII-8BIT unless explicitly
     |  specified.
-----+--------------------------------------------------------
"t"  |  Text file mode (may appear with
     |  any of the key letters listed above except "b").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment