From 95e13c6180e2da3a377e1bad0205b101e1b2abad Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 3 Jun 2022 12:53:50 -0500 Subject: [PATCH] Add Use IRB And Ruby Flags With Rails Console as a Rails TIL --- README.md | 3 ++- ...e-irb-and-ruby-flags-with-rails-console.md | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 rails/use-irb-and-ruby-flags-with-rails-console.md diff --git a/README.md b/README.md index b84ec32..f4ba38d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1214 TILs and counting..._ +_1215 TILs and counting..._ --- @@ -809,6 +809,7 @@ _1214 TILs and counting..._ - [Truncate Almost All Tables](rails/truncate-almost-all-tables.md) - [Update Column Versus Update Attribute](rails/update-column-versus-update-attribute.md) - [Upgrading Your Manifest For Sprocket's 4](rails/upgrading-your-manifest-for-sprockets-4.md) +- [Use IRB And Ruby Flags With Rails Console](rails/use-irb-and-ruby-flags-with-rails-console.md) - [Verify And Read A Signed Cookie Value](rails/verify-and-read-a-signed-cookie-value.md) - [Where Am I In The Partial Iteration?](rails/where-am-i-in-the-partial-iteration.md) - [Wipe Out All Precompiled Assets](rails/wipe-out-all-precompiled-assets.md) diff --git a/rails/use-irb-and-ruby-flags-with-rails-console.md b/rails/use-irb-and-ruby-flags-with-rails-console.md new file mode 100644 index 0000000..b884852 --- /dev/null +++ b/rails/use-irb-and-ruby-flags-with-rails-console.md @@ -0,0 +1,22 @@ +# Use IRB And Ruby Flags With Rails Console + +The `rails console` command only accepts two flags `--environment` and +`--[no-]sandbox`. However, the `rails console` is built on top of IRB and Ruby, +so it can accept flags that those commands understand. + +You can't pass an IRB flag directly to `rails console` though because it +doesn't consider those part of its repertoire. + +```bash +$ rails console --sandbox -r './vendor/setup_records.rb' +``` + +It will put the session in sandbox mode, but it doesn't know what to do with +`-r`. We have to explicitly separate its flags from flags that should be +passed through. This is done with the `--` CLI convention. + +```bash +$ rails console --sandbox -- -r './vendor/setup_records.rb' +``` + +Everything after the `--` gets passed along to IRB.