Chapter 3 - Typing Variables and Controlling the Flow
Hello again! Now that I am off the post vaccine unpleasantries, I will put in my thoughts on Chapter 3, I skimmed it once, will get down with the code after brunch. It mostly deals with types, I got to meet a new type of method name, to_i*
, to_f
, to_a
etc. The way to encourage types on number is similar, 25_i8
, also, we have ?
suffix to nullity management.
In the middle there’s a discussion on exception handling. So two things I usually check for when talking about exceptions: 1. How do you do an “else if” and 2. What are the exception handling syntax. So for 1. It’s elsif
(Not elif
or else if
) and for 2. It’s begin..rescue...else...ensure
and raise
to throw.
An interesting discovery:
x = 10
ten_1 = if x == 10 "It's TEN" else false end # Works fine => Bool | String
ten_2 = if x == 10 1 else false end # Works fine => Bool | Int32
ten_3 = if x == 10 true else false end # ERROR: Unexpected token: `true`
ten_ternary = x == 10 ? true : false # I know it's redundant, but the syntax works as expected
Maybe I am missing something here. Another thing, I really really miss a REPL. Maybe it’s just me.
Another thing, if x == 10 10 else false end
works, putting the condition and expression in the same line, but in case of case, case x when 10 10 else false
does not work, it should be case x ; when 10 ; 10 ; false end
. I just wish ;
being relevant for if
too could have been more consistent?
Also, the book was written in a time when there was case...when
in Crystal. We now have case..in
that is exhaustive. Also, the case does some than the C/Java ones do, for instance, you can destruct tuples, and call bool methods (i.e. when { .odd?, .even? } =>
) and does type check. Not sure I mentioned this in an earlier post because I recall the book introducing case
at an earlier section too.
Here’s a nice example from the book:
(1..100).each do |i|
case {i % 3, i % 5}
when {0, 0}
puts "FizzBuzz"
when {0, _}
puts "Fizz"
when {_, 0}
puts "Buzz"
else
puts i
end
end
One minor note, I found the name responds_to?
method really cute, quite in line with how the original OOP used to be about messages and passing them around.
Middle of the chapter contained an example that glued all the knowledge together, and after that there was an introduction to composite data types. There’s Hash key => value
and named tuple name: value
, that was nice. There’s a type for Set, unlike Go.
So once you know the control flow, assignments and typings of a language, your knowledge of that language is somewhat Turing complete, you can do anything with it (though it’d not be pleasant). This chapter gets you there about Crystal, preparing you for the next step: organization. This really expands upon the first 50% of the previous chapter.
As usual, there was a Company’s story through an interview at the end. This time it is Dev Demand which mostly praised the performance and intuitiveness. There was tad comparison with PHP, Python and Go. To be honest, I think Crystal is intuitive for Ruby devs, just as Go is intuitive for people only habituated with C-Family members and Elixir for anyone who got habituated with patterns. For me, Crystal isn’t intuitive, but I’m sure Ruby will become once I learn it AFTER (if ever) Crystal