---
title: 'This week in #dev (Dec 2, 2022)'
teaser: 'Running shell commands in Ruby, truly random numbers, fixing flaky tests,
  and how to get the most out of TypeScript.

  '
tags: this week in dev,testing,ruby,typescript,til,tip
author: thoughtbot
published_on: 2022-12-20
---

Hey! Welcome to another edition of This Week in #dev, a series of posts where we
bring some of the most interesting Slack conversations to the public. Today
we'll talk about randomness, shell commands, flaky tests, and TypeScript. Stick
around!

## Running shell commands in Ruby

You can return standard output from system commands executed in Ruby by
wrapping them in [backticks], as shown by [Steve Polito]:

```ruby
# Calling system will return true, false or nil.
system("pgrep ruby")
# 76532
# => true

# Using a backtick returns the standard output.
`pgrep ruby`
# => "76532\n"
```

[Thiago Silva] points out that you can use `$?` to check the exit status of the
last command:

```rb
`echo oops && exit 99`   #=> "oops\n"
$?.exitstatus            #=> 99
```

If you require the `English` library, you can use `$CHILD_STATUS` instead of
`$?`. It adds a [few other aliases], if you're interested.

```rb
`echo oops && exit 99`   #=> "oops\n"
$CHILD_STATUS.success?   #=> false
```

[backticks]: https://rubyapi.org/3.1/o/kernel#method-i-60
[few other aliases]: https://docs.ruby-lang.org/en/3.1/English.html

<aside class="info">
  If you need to safely execute shell commands (even user-provided ones!), you
  can use our <a href="https://github.com/thoughtbot/terrapin">Terrapin gem</a>.
  Thanks, Neil, for providing the shameless plug!
</aside>

## random.org

[Matheus Richard] learned about <https://www.random.org>, a "true number
generator". The randomness comes from atmospheric noise, which for many
purposes, it's claimed to be better than the pseudo-random number algorithms
typically used in computer programs.

If pseudo-random is enough for you, [Neil Carvalho] brings up `/dev/random` and
`/dev/urandom`, that have entropy sources from some hardware events, among
others. Also, here's [our guide on randomization].

[our guide on randomization]: https://github.com/thoughtbot/guides/blob/main/security/application.md#randomization

## Do you have flaky system tests?

Richard Newman shares an article about [how to deal with flaky system tests] in
Rails. Tip: `sleep`ing is not the answer.

[how to deal with flaky system tests]: https://naildrivin5.com/blog/2022/11/29/dealing-with-flaky-tests.html

## Taking the most out of TypeScript

If you're coming from a dynamically-typed language to TypeScript, [Stephen Hanson]
has some tips for you:

- It’s essential that you use an editor that has TypeScript tooling (i.e. that
  runs a TypeScript language server). You should be able to see TypeScript
  errors inline, hover over variables to see what type they are, and get
  auto-completion as you access properties on objects. If your editor does not
  have this tooling, then TypeScript is serving little value for you, and it
  will be a frustrating experience identifying errors only when you manually
  compile the code. VS Code has all this built-in by default.

- To understand why the page loads fine but TypeScript errors, it helps to see
  how TypeScript works. TypeScript types are there to help the development
  experience. When your code is compiled, the types are completely stripped
  away, and the result is regular JavaScript. The types are not present at all
  at run-time. Changing a type will not break or fix your app. The TypeScript
  errors you get are there for the developer but don’t affect the end result.

He also indicates a caveat for point two: "I think some apps are set up so that compiling
to JavaScript fails if there are type errors, but that is not the norm."

[Matheus Richard] adds that [you can make the compiler stricter] to catch more
potential errors.

[you can make the compiler stricter]: https://www.typescriptlang.org/tsconfig#strict

## Thanks

This edition was brought to you by [Matheus Richard], [Neil Carvalho], Richard
Newman, [Stephen Hanson], [Steve Polito], and [Thiago Silva]. Thanks to all
contributors! 🎉

[matheus richard]: https://thoughtbot.com/blog/authors/matheus-richard
[neil carvalho]: https://thoughtbot.com/blog/authors/neil-carvalho
[stephen hanson]: https://shanson.co/
[steve polito]: https://thoughtbot.com/blog/authors/steve-polito
[thiago silva]: https://thoughtbot.com/blog/authors/thiago-araujo-silva
