Using Basic Variables and Types

Programming is largely about moving data from place to place, and you’ll need containers for that data. Crystal’s variables can store different kinds of data. As in most languages, you assign them with the = sign.

 name = ​"Diamond"
 element = ​'C'
 hardness = 10

Crystal assignment does more than just put a value into the variable, however. The compiler infers the type of a variable from the value(s) assigned. You can see the variable types the compiler inferred—through type reflection—by testing with the typeof expression.

 puts typeof(name) ​# => String
 puts typeof(element) ​# => Char
 puts typeof(hardness) ​# => Int32

Crystal interpreted the name variable as a String because you used double quotes. It interpreted element as a Char (a single character value) because you used single quotes. It interpreted hardness as an integer, specifically a 32-bit integer, because 10 is a short number with no decimal point. (typeof is a great diagnostic tool, but if you’re building it into production code, you’re working against Crystal’s emphasis on managing type details during compilation.)

Most of the time, you can trust Crystal to infer the variable type you want. If you want to be more explicit about it, you can tell the compiler what you want, and the compiler will enforce it for you.

 hardness : Int32
 hardness = 10
 puts typeof(hardness) ​# => Int32
 hardness = ​"20"​ ​# => Error... type must be Int32, not (Int32 | String)

As the first line of that shows, you can declare a variable and its type before assigning it. The Crystal compiler will then use that type in deciding whether later assignments are appropriate. But you can’t use that variable for anything before assigning a value to it. (Unless you explicitly define the type of a variable, Crystal will let you assign values of different types to it, and the compiler will keep up if possible.)

You can also declare and assign in the same line, like hardness : Int32 = 10. (Int32 is a signed 32-bit integer, and the spaces around the colon are required.) Remember, however, that you can let Crystal infer variable types most of the time, and you don’t need to fill your code with type declarations.

When it’s convenient, you can also assign values to multiple variables at one time.

 name, element, hardness = ​"Diamond"​, ​'C'​, 10

If you’d prefer, you can also put multiple statements on one line if you separate them with a semicolon.

 name = ​"Diamond"​; element = ​'C'​; hardness = 10

Semicolons enable you to put multiple statements of any kind on a line, not just variable assignments.

If you’re in a hurry, you can also switch or swap the values of two variables in one line:

 # swap
 n = 41
 m = 42
 n, m = m, n
 n ​# => 42
 m ​# => 41

(Behind the scenes, Crystal’s compiler creates a temporary variable to make this work.)

You may want to create named values that take only one value, called constants. Normal variables, which can change value, start with lowercase letters and use underscores for spaces, called snake or underscore case. Constants, which can’t change value, start with uppercase letters, and they’re traditionally written in all capitals with underscores for spaces. If you try to set a value for them after they’ve been set, the Crystal compiler will protest.

 DIAMOND_HARDNESS = 10
 DIAMOND_HARDNESS = 20 ​# => already initialized constant DIAMOND_HARDNESS

Crystal also includes some starkly limited types that are useful for logical operations. Boolean variables can accept the values of true and false, while nil (of type Nil) explicitly means there is no value.

Not Global

images/aside-icons/tip.png

Ruby supports global variables whose names start with $. Crystal’s variables are always locally scoped, and it has no global variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.66.185