mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Compare commits
2 Commits
16074c021f
...
e2eb31a4a9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e2eb31a4a9 | ||
|
|
113b7b2dfe |
@@ -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).
|
||||
|
||||
_1681 TILs and counting..._
|
||||
_1683 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -710,6 +710,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Keyboard Shortcuts For Interesting With Text Areas](mac/keyboard-shortcuts-for-interacting-with-text-areas.md)
|
||||
- [List All The Say Voices](mac/list-all-the-say-voices.md)
|
||||
- [Open Finder.app To Specific Directory](mac/open-finder-app-to-specific-directory.md)
|
||||
- [Prevent Sleep With The Caffeinate Command](mac/prevent-sleep-with-the-caffeinate-command.md)
|
||||
- [Quickly Type En Dashes And Em Dashes](mac/quickly-type-en-dashes-and-em-dashes.md)
|
||||
- [Require Additional JS Libraries In Postman](mac/require-additional-js-libraries-in-postman.md)
|
||||
- [Resize App Windows With AppleScript](mac/resize-app-windows-with-applescript.md)
|
||||
@@ -1419,6 +1420,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Rake Only Lists Tasks With Descriptions](ruby/rake-only-lists-tasks-with-descriptions.md)
|
||||
- [Read The First Line From A File](ruby/read-the-first-line-from-a-file.md)
|
||||
- [Refer To Implicit Block Argument With It](ruby/refer-to-implicit-block-argument-with-it.md)
|
||||
- [Reference Hash Key With Safe Navigation](ruby/reference-hash-key-with-safe-navigation.md)
|
||||
- [Rendering ERB](ruby/rendering-erb.md)
|
||||
- [Replace The Current Process With An External Command](ruby/replace-the-current-process-with-an-external-command.md)
|
||||
- [Require Entire Gemfile In Pry Session](ruby/require-entire-gemfile-in-pry-session.md)
|
||||
|
||||
30
mac/prevent-sleep-with-the-caffeinate-command.md
Normal file
30
mac/prevent-sleep-with-the-caffeinate-command.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# Prevent Sleep With The Caffeinate Command
|
||||
|
||||
MacOS has a built-in utility `caffeinate` that can programatically prevent your
|
||||
machine from sleeping. There are two kinds of sleep that it can prevent via
|
||||
_assertions_.
|
||||
|
||||
> caffeinate creates assertions to alter system sleep behavior.
|
||||
|
||||
The two kinds of sleep behavior are _display sleep_ and _system idle sleep_. An
|
||||
assertion to prevent display sleep can be created with `-d` and system idle
|
||||
sleep with `-i`.
|
||||
|
||||
We can combine those to prevent both and then specify a duration (_timeout_)
|
||||
with `-t` (with a value in seconds).
|
||||
|
||||
```bash
|
||||
caffeinate -d -i -t 600
|
||||
```
|
||||
|
||||
This creates assertions with 10 minute timeouts for both display and system idle
|
||||
sleep.
|
||||
|
||||
The `caffeinate` command is blocking, so if you want to start it in the
|
||||
background, you can do that like so:
|
||||
|
||||
```bash
|
||||
caffeinate -d -i -t 600 &
|
||||
```
|
||||
|
||||
See `man caffeinate` for more details.
|
||||
30
ruby/reference-hash-key-with-safe-navigation.md
Normal file
30
ruby/reference-hash-key-with-safe-navigation.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# Reference Hash Key With Safe Navigation
|
||||
|
||||
Let's say we have a variable that we expect to be a hash, but could also be
|
||||
`nil`. We want to try to grab a value from that hash by referencing a specific
|
||||
key. Because it could be `nil`, we cannot simply do:
|
||||
|
||||
```ruby
|
||||
stuff[:key]
|
||||
```
|
||||
|
||||
As that could result in `NoMethodError: undefined method '[]' for nil
|
||||
(NoMethodError)`.
|
||||
|
||||
We should use the _safe navigation_ operator (`&`) to avoid raising that error.
|
||||
However, we should pay attention to a necessary syntax shift from the short-hand
|
||||
`[:key]` to the long-hand `[](:key)`.
|
||||
|
||||
```ruby
|
||||
stuff&.[](:key)
|
||||
```
|
||||
|
||||
The meaning of this syntax is that we are calling the `#[]` method and we pass
|
||||
it a single argument `:key` wrapped in parentheses.
|
||||
|
||||
Another approach would be to use `#dig` which can feel more ergonomic than the
|
||||
above syntax switch.
|
||||
|
||||
```ruby
|
||||
stuff&.dig(:key)
|
||||
```
|
||||
Reference in New Issue
Block a user