Skip to content

Latest commit

 

History

History
270 lines (169 loc) · 5.85 KB

other.md

File metadata and controls

270 lines (169 loc) · 5.85 KB

Other

{id: other}

Sequence

{id: sequence-generation}

Check if variable is nil?

{id: nil} {i: nil} {i: nil?}

Single quotes vs double quotes

{id: single-quotes-vs-double-quotes}

  • Single quotes are for characters
  • Double quotes are for strings

No type-checking?

{id: no-typechecking}

Divide by zero is Infinity

{id: divide-by-zero-is-infinity} {i: Infinity}

Require other files

{id: require-other-files}

List Methods

{id: list-methods}

Checking the slides

{id: checking-the-slides}

Crystal mine

{id: crystal-mine}

Ameba Linter

{id: ameba-linter}

shards install
./bin/ameba

Gravatar

{id: gravatar} {i: Digest::MD5.hexdigest}

Try Crystal

{id: try}

STDERR, STDOUT

{id: stderr} {i: STDERR} {i: STDOUT}

  • Redirection on the command line using > out and 2> err

Return Boolean

{id: return-boolean}

Symbols

{id: symbols}

Docspec

{id: docspec}

require

{id: require}

{aside} require - imports are global. This makes it hard to see in a file where some objects might come from as they might have been required by some other file.

Similarly requiring a bunch of files in a directory is easy to do, but might make it a bit harder for someone without an IDE to find the source of each object. {/aside}

require "./directory/file"  # relative path to the cr file
require "./directory/*"     # all the cr files in the directory
require "./directory/**"    # all the cr files in the directory - recursively
require "some_name"         # Find it somewhere (standard library, src directory)
  • You can put the require statements anywhere but you might want to be consistent in your project.
  • Make sure you don't have circular requires.

Constants

{id: constants}

  • Variable names that start with upper-case letter are constants

Multiple assignment

{id: multiple-assignment}

Chained assignment

{id: chained-assignment}

Int methods: Times

{id: times}

Int64 Zero

{id: int64-zero}

Question mark: ?

{id: question-mark} {i: ?}

  • meaning "or nil" in type definitions String? is the same as String | Nil
  • Methods ending with ? usually return a boolean (true, false) - there is no enforcement of this in Crystal
  • If a construct might raise an exception adding a question mark can convert that into returning nil
  • It is also part of the conditional operator ?:

Exclamation mark: !

{id: exclamation-mark} {i: !}

  • Methods ending with ! usually modify the underlying object.
  • Logical not (before an expression)

Ampersand: &

{id: ampersand} {i: &}

STDIN don't accept nil

{id: stdin-dont-accept-nil}

  • gets will retun nil if we press Ctrl-D
  • gets.not_nil! will raise an exception

Math

{id: math}

Proc

{id: proc} {i: Proc}

Enums

{id: enums} {i: enum}

Type of array elements

{id: type-of-array-elements}

All the elements of an array "inherit" the type from the array

Environment variables

{id: environment-variables} {i: ENV}

Int32 or Nil

{id: int32-or-nil}

Resources

{id: resources}