Last active
August 29, 2015 14:27
-
-
Save flou/683444f28e0d474d229b to your computer and use it in GitHub Desktop.
Learn Crystal in Y minutes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Comments start with a hash | |
# First and foremost: Everything is an object. | |
# Numbers are objects | |
3.class # => Int32 | |
3.to_s # => "3" | |
# Some basic arithmetic | |
1 + 1 # => 2 | |
8 - 1 # => 7 | |
10 * 2 # => 20 | |
35 / 5 # => 7 | |
2**5 # => 32 | |
# Arithmetic is just syntactic sugar | |
# for calling a method on an object | |
1.+(3) # => 4 | |
10.* 5 # => 50 | |
# Special values are objects | |
nil # Nothing to see here | |
true # truth | |
false # falsehood | |
nil.class # => Nil | |
true.class # => Bool | |
false.class # => Bool | |
# Equality | |
1 == 1 # => true | |
2 == 1 # => false | |
# Inequality | |
1 != 1 # => false | |
2 != 1 # => true | |
# apart from false itself, nil is the only other 'falsey' value | |
!nil # => true | |
!false # => true | |
!0 # => false | |
# More comparisons | |
1 < 10 # => true | |
1 > 10 # => false | |
2 <= 2 # => true | |
2 >= 2 # => true | |
# Logical operators | |
true && false # => false | |
true || false # => true | |
!true # => false | |
# Chars represent a single character, they are created with single quotes | |
'a'.class # => Char | |
# Chars can store any unicode codepoint with hexadecimal representation | |
'\u0041' # => A | |
'\u{1F52E}' # => 🔮 | |
'single quote are not for Strings' # => unterminated char literal, use double quotes for strings | |
# Strings are immutable objects, they are a sequence of Chars and they are created using double quotes | |
"I am a string".class # => String | |
"This is a string"[0].class # => Char | |
placeholder = "use string interpolation" | |
"I can #{placeholder} inside strings" | |
# => "I can use string interpolation inside strings" | |
"%s can be %s the %s way" % ["Strings", "interpolated", "old"] | |
# => Strings can be interpolated the old way | |
# Combine strings, but not with numbers | |
"hello " + "world" # => "hello world" | |
"hello " + 3 # => no overload matches 'String#+' with types Int32 | |
"hello " + 3.to_s # => "hello 3" | |
# `puts` prints to the output | |
puts "I'm printing!" | |
# `pp` prints the expression with its result | |
pp "Hello" * 2 # => "Hello" * 2 = "HelloHello" | |
# A Char can be appended to a String object, but not the other way around | |
"I like " + '\u{1F363}' # => I like 🍣 | |
'\u{1F363}' + "is good" # => undefined method '+' for Char | |
# Variables | |
x = 25 # => 25 | |
x # => 25 | |
# Note that assignment returns the value assigned | |
# This means you can do multiple assignment: | |
x = y = 10 # => 10 | |
x # => 10 | |
y # => 10 | |
# By convention, use snake_case for variable names | |
snake_case = true | |
# Use descriptive variable names | |
path_to_project_root = "/good/name/" | |
path = "/bad/name/" | |
# Integers | |
# There are 4 signed integer types Int8, Int16, Int32 and Int64 | |
# and 4 unsigned integer types UInt8, UInt16, UInt32 and UInt64 | |
# If unspecified, the literal's type is chosen by the compiler to fit the number | |
314 # => Int32 | |
9223372036854775808 # => UInt64 | |
# Dashes can be added to improve code readability | |
1_000_000 # => Int32 | |
# The integer's type can be specified by adding _iXX (signed) or _uXX (unsigned) | |
10_i8 # Int8 | |
10_u64 # => UInt64 | |
# Binary start with 0b | |
0b1101 # => 13 | |
# Octal numbers start with 0o | |
0o123 # => 83 | |
# Hexadecimal numbers start with 0x | |
0xfa1 # => 4001 | |
# Symbols (are objects) | |
# Symbols are immutable, reusable constants represented internally by an | |
# integer (Int32) value. | |
:pending.class # => Symbol | |
status = :pending | |
status == :pending # => true | |
status == "pending" # => false | |
status == :approved # => false | |
# Arrays | |
# You need to declare in advance what the array will contain with the `of` keyword | |
array = [] of Int32 # => Array(Int32) | |
array = [] of SignedInt # => Array(Int8 | Int32 | Int16 | Int64) | |
array = [] of UnsignedInt # => Array(UInt8 | UInt32 | UInt16 | UInt64) | |
# This is an array of Int32 | |
array = [1, 2, 3, 4, 5] # => [1, 2, 3, 4, 5] | |
array.class # => Array(Int32) | |
# You can combine types | |
array = [] of Int32 | String | Float32 # => Array(String | Int32 | Float32) | |
# Types can also be guessed at declaration time | |
[1, "hello", false] # => [1, "hello", false] | |
[1, "hello", false].class # => Array(String | Int32 | Bool) | |
# Arrays can be indexed | |
# From the front | |
array[0] # => 1 | |
array[12] # => IndexError | |
array[12]? # => nil | |
# Like arithmetic, [var] access | |
# is just syntactic sugar | |
# for calling a method [] on an object | |
array.[] 0 # => 1 | |
array.[] 12 # => IndexError | |
array.[]? 12 # => nil | |
# From the end | |
array[-1] # => 5 | |
# With a start index and length | |
array[2, 3] # => [3, 4, 5] | |
# Or with a range | |
array[1..3] # => [2, 3, 4] | |
# Add to an array like this | |
array << 6 # => [1, 2, 3, 4, 5, 6] | |
# Check if an item exists in an array | |
array.includes?(1) # => true | |
# Hashes are Crystal's primary dictionary with keys/value pairs. | |
# Hashes are denoted with curly braces: | |
hash = { "color" => "green", "number" => 5 } | |
hash.class # => Hash(String, String | Int32) | |
hash.keys # => ['color', 'number'] | |
# Hashes can be quickly looked up by key: | |
hash["color"] # => "green" | |
hash["number"] # => 5 | |
# Asking a hash for a key that doesn't exist raises KeyError | |
# or it returns nil with []?: | |
hash["nothing here"] # => Missing hash value: "nothing here" KeyError | |
hash["nothing here"]? # => nil | |
# Hashes can have symbols as keys | |
new_hash = { defcon: 3, action: true } # => Hash(Symbol, (Int32 | Bool)) | |
new_hash.keys # => [:defcon, :action] | |
# Check existence of keys in hash | |
new_hash.has_key?(:defcon) # => true | |
# The types of the keys and values must be given when declaring a new hash | |
hash = {} of Symbol => (Int32 | Bool | String) | |
hash.merge!({ defcon: 3, action: true }) | |
hash[:color] = "blue" | |
# => {:defcon => 3, :action => true, :color => "blue"} | |
hash.merge({ color: "red" }) | |
# => {:defcon => 3, :action => true, :color => "red"} | |
# Tip: Both Arrays and Hashes are Enumerable | |
# They share a lot of useful methods such as each, map, count, and more | |
[1, 2, 3, 4, 5].map { |i| i**2 } | |
# => [1, 4, 9, 16, 25] | |
# This can be rewritten using the &. notation | |
[1, 2, 3, 4, 5].map(&.**(2)) | |
# => [1, 4, 9, 16, 25] | |
hash = {"one": 1, "two": 2, "three": 3} | |
hash.map { |k, v| k.upcase } | |
# => ["ONE", "TWO", "THREE"] | |
# Control structures | |
# if returns the value of the last evaluated block | |
statement = if true | |
"if statement" | |
elsif false | |
"else if, optional" | |
else | |
"else, also optional" | |
end | |
puts statement # => if statement | |
# if and unless can be placed at the end of the expression | |
"2 equals 2" if 2 == 2 | |
"3 does not equal 2" unless 3 == 2 | |
# The "each" method of a range runs the block once for each element of the range. | |
# The block is passed a counter as a parameter. | |
# Calling the "each" method with a block looks like this: | |
(1..5).each do |counter| | |
puts "iteration #{counter}" | |
end | |
# => iteration 1 | |
# => iteration 2 | |
# => iteration 3 | |
# => iteration 4 | |
# => iteration 5 | |
# You can also surround blocks in curly brackets: | |
(1..5).each { |counter| puts "iteration #{counter}" } | |
# The contents of data structures can also be iterated using each. | |
array.each do |element| | |
puts "#{element} is part of the array" | |
end | |
hash.each do |key, value| | |
puts "#{key} is #{value}" | |
end | |
counter = 1 | |
while counter <= 5 | |
puts "iteration #{counter}" | |
counter += 1 | |
end | |
# => iteration 1 | |
# => iteration 2 | |
# => iteration 3 | |
# => iteration 4 | |
# => iteration 5 | |
grade = 'B' | |
case grade | |
when 'A' | |
puts "Way to go kiddo" | |
when 'B' | |
puts "Better luck next time" | |
when 'C' | |
puts "You can do better" | |
when 'D' | |
puts "Scraping through" | |
when 'F' | |
puts "You failed!" | |
else | |
puts "Alternative grading system, eh?" | |
end | |
# => "Better luck next time" | |
# cases can also use ranges | |
grade = 82 | |
case grade | |
when 90..100 | |
puts "Hooray!" | |
when 80...90 | |
puts "OK job" | |
else | |
puts "You failed!" | |
end | |
# => "OK job" | |
# exception handling: | |
class NoMemoryError < Exception; end | |
class RuntimeError < Exception; end | |
begin | |
# code here that might raise an exception | |
raise NoMemoryError.new("You ran out of memory.") | |
rescue exception_variable : NoMemoryError | |
puts "NoMemoryError was raised", exception_variable | |
rescue other_exception_variable : RuntimeError | |
puts "RuntimeError was raised now" | |
else | |
puts "This runs if no exceptions were thrown at all" | |
ensure | |
puts "This code always runs no matter what" | |
end | |
# Functions | |
def double(x) | |
x * 2 | |
end | |
# Functions (and all blocks) implicitly return the value of the last statement | |
double(2) # => 4 | |
# Parentheses are optional where the result is unambiguous | |
double 3 # => 6 | |
double double 3 # => 12 | |
def sum(x, y) | |
x + y | |
end | |
# Method arguments are separated by a comma | |
sum 3, 4 # => 7 | |
sum sum(3, 4), 5 # => 12 | |
# yield | |
# All methods have an implicit, optional block parameter | |
# it can be called with the 'yield' keyword | |
def surround | |
puts '{' | |
yield | |
puts '}' | |
end | |
surround { puts "hello world" } | |
# { | |
# hello world | |
# } | |
# You can pass a list of arguments, which will be converted into a Tuple | |
# That's what splat operator ("*") is for | |
def guests(*array) | |
# array is Tuple(Int32, Int32, Int32) | |
array.each { |guest| puts guest } | |
end | |
guests(1, 2, 3) | |
# Define a class with the class keyword | |
class Human | |
# A class variable. It is shared by all instances of this class. | |
@@species = "H. sapiens" | |
# Basic initializer | |
def initialize(@name, @age = 0) | |
end | |
# Basic setter method | |
def name=(name) | |
@name = name | |
end | |
# Basic getter method | |
def name | |
@name | |
end | |
# The above functionality can be encapsulated using the attr_accessor method as follows | |
property name | |
# Getter/setter methods can also be created individually like this | |
getter name | |
setter name | |
# A class method uses self to distinguish from instance methods. | |
# It can only be called on the class, not an instance. | |
def self.say(msg) | |
puts msg | |
end | |
def species | |
@@species | |
end | |
end | |
# Instantiate a class | |
rose = Human.new("Tose Tyler") | |
clara = Human.new("Clara Oswald") | |
# Let's call a couple of methods | |
rose.species # => "H. sapiens" | |
rose.name # => "Rose Tyler" | |
rose.name = "The Bad Wolf" # => "The Bad Wolf" | |
rose.name # => "The Bad Wolf" | |
clara.species # => "H. sapiens" | |
clara.name # => "Clara Oswald" | |
# Call the class method | |
Human.say("Hello humanity") # => "Hello humanity" | |
module ModuleExample | |
def foo | |
"foo" | |
end | |
end | |
# Including modules binds their methods to the class instances | |
# Extending modules binds their methods to the class itself | |
class Person | |
include ModuleExample | |
end | |
class Book | |
extend ModuleExample | |
end | |
Person.new.foo # => 'foo' | |
Book.foo # => 'foo' | |
Person.foo # => NoMethodError: undefined method `foo' for Person:Class | |
Book.new.foo # => NoMethodError: undefined method `foo' for Book |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment