From 256376923e46315507f06121e65ac0b1f29b1d2a Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 25 Mar 2019 16:59:53 -0500 Subject: [PATCH] Add Encode A String As URL-Safe Base64 as a ruby til --- README.md | 3 ++- ruby/encode-a-string-as-url-safe-base64.md | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 ruby/encode-a-string-as-url-safe-base64.md diff --git a/README.md b/README.md index 9bd0a8f..582672d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_795 TILs and counting..._ +_796 TILs and counting..._ --- @@ -615,6 +615,7 @@ _795 TILs and counting..._ - [Double Splat To Merge Hashes](ruby/double-splat-to-merge-hashes.md) - [Edit Previous Parts Of The Pry Buffer History](ruby/edit-previous-parts-of-the-pry-buffer-history.md) - [Editing Code In Pry](ruby/editing-code-in-pry.md) +- [Encode A String As URL-Safe Base64](ruby/encode-a-string-as-url-safe-base64.md) - [Evaluating One-Off Commands](ruby/evaluating-one-off-commands.md) - [FactoryGirl Sequences](ruby/factory-girl-sequences.md) - [Fail](ruby/fail.md) diff --git a/ruby/encode-a-string-as-url-safe-base64.md b/ruby/encode-a-string-as-url-safe-base64.md new file mode 100644 index 0000000..bfa82de --- /dev/null +++ b/ruby/encode-a-string-as-url-safe-base64.md @@ -0,0 +1,19 @@ +# Encode A String As URL-safe Base64 + +Ruby's standard lib comes with a [Base64 +module](https://ruby-doc.org/stdlib-2.1.4/libdoc/base64/rdoc/Base64.html) +with a number of utilities for encoding and decoding data as +[Base64](https://en.wikipedia.org/wiki/Base64). One of the methods it +provides is `urlsafe_encode64`. + +```ruby +> require 'base64' +true +> Base64.urlsafe_encode64('hello') +"aGVsbG8=" +> Base64.urlsafe_encode64('1') +"MQ==" +``` + +You can pass it any string and it will create a URL-safe Base64 encoded +representation of that string.