mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
[crystal/en] Update to Crystal 1.0 (#4235)
Crystal 1.0 was released this year (currently at 1.1.1 actually), which solidified the language. Updating based on language changes.
This commit is contained in:
parent
7f5d9b460a
commit
7e5abe2aaf
@ -4,6 +4,7 @@ filename: learncrystal.cr
|
|||||||
contributors:
|
contributors:
|
||||||
- ["Vitalii Elenhaupt", "http://veelenga.com"]
|
- ["Vitalii Elenhaupt", "http://veelenga.com"]
|
||||||
- ["Arnaud Fernandés", "https://github.com/TechMagister/"]
|
- ["Arnaud Fernandés", "https://github.com/TechMagister/"]
|
||||||
|
- ["Valentin Baca", "https://github.com/valbaca/"]
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -64,24 +65,25 @@ true.class #=> Bool
|
|||||||
1.5e10.class #=> Float64
|
1.5e10.class #=> Float64
|
||||||
1.5e-7.class #=> Float64
|
1.5e-7.class #=> Float64
|
||||||
|
|
||||||
# Chars
|
# Chars use 'a' pair of single quotes
|
||||||
|
|
||||||
'a'.class #=> Char
|
'a'.class #=> Char
|
||||||
|
|
||||||
# Octal codepoint
|
# Chars are 32-bit unicode
|
||||||
'\101' #=> 'A' : Char
|
'あ' #=> 'あ' : Char
|
||||||
|
|
||||||
# Unicode codepoint
|
# Unicode codepoint
|
||||||
'\u0041' #=> 'A' : Char
|
'\u0041' #=> 'A' : Char
|
||||||
|
|
||||||
# Strings
|
# Strings use a "pair" of double quotes
|
||||||
|
|
||||||
"s".class #=> String
|
"s".class #=> String
|
||||||
|
|
||||||
# Strings are immutable
|
# Strings are immutable
|
||||||
s = "hello, " #=> "hello, " : String
|
s = "hello, " #=> "hello, " : String
|
||||||
s.object_id #=> 134667712 : UInt64
|
s.object_id #=> 134667712 : UInt64
|
||||||
s += "Crystal" #=> "hello, Crystal" : String
|
s += "Crystal"
|
||||||
|
s #=> "hello, Crystal" : String
|
||||||
s.object_id #=> 142528472 : UInt64
|
s.object_id #=> 142528472 : UInt64
|
||||||
|
|
||||||
# Supports interpolation
|
# Supports interpolation
|
||||||
@ -89,7 +91,8 @@ s.object_id #=> 142528472 : UInt64
|
|||||||
|
|
||||||
# Multiline string
|
# Multiline string
|
||||||
"This is
|
"This is
|
||||||
multiline string"
|
multiline string" #=> "This is\n multiline string"
|
||||||
|
|
||||||
|
|
||||||
# String with double quotes
|
# String with double quotes
|
||||||
%(hello "world") #=> "hello \"world\""
|
%(hello "world") #=> "hello \"world\""
|
||||||
@ -110,7 +113,7 @@ sentence == "question?" #=> false : Bool
|
|||||||
# Arrays
|
# Arrays
|
||||||
|
|
||||||
[1, 2, 3].class #=> Array(Int32)
|
[1, 2, 3].class #=> Array(Int32)
|
||||||
[1, "hello", 'x'].class #=> Array(Int32 | String | Char)
|
[1, "hello", 'x'].class #=> Array(Char | Int32 | String)
|
||||||
|
|
||||||
# Empty arrays should specify a type
|
# Empty arrays should specify a type
|
||||||
[] # Syntax error: for empty arrays use '[] of ElementType'
|
[] # Syntax error: for empty arrays use '[] of ElementType'
|
||||||
@ -154,24 +157,24 @@ array.includes? 3 #=> true
|
|||||||
|
|
||||||
# There is a special array syntax with other types too, as long as
|
# There is a special array syntax with other types too, as long as
|
||||||
# they define a .new and a #<< method
|
# they define a .new and a #<< method
|
||||||
set = Set{1, 2, 3} #=> [1, 2, 3]
|
set = Set{1, 2, 3} #=> Set{1, 2, 3}
|
||||||
set.class #=> Set(Int32)
|
set.class #=> Set(Int32)
|
||||||
|
|
||||||
# The above is equivalent to
|
# The above is equivalent to
|
||||||
set = Set(typeof(1, 2, 3)).new
|
set = Set(typeof(1, 2, 3)).new #=> Set{} : Set(Int32)
|
||||||
set << 1
|
set << 1 #=> Set{1} : Set(Int32)
|
||||||
set << 2
|
set << 2 #=> Set{1, 2} : Set(Int32)
|
||||||
set << 3
|
set << 3 #=> Set{1, 2, 3} : Set(Int32)
|
||||||
|
|
||||||
# Hashes
|
# Hashes
|
||||||
|
|
||||||
{1 => 2, 3 => 4}.class #=> Hash(Int32, Int32)
|
{1 => 2, 3 => 4}.class #=> Hash(Int32, Int32)
|
||||||
{1 => 2, 'a' => 3}.class #=> Hash(Int32 | Char, Int32)
|
{1 => 2, 'a' => 3}.class #=> Hash(Char| Int32, Int32)
|
||||||
|
|
||||||
# Empty hashes should specify a type
|
# Empty hashes must specify a type
|
||||||
{} # Syntax error
|
{} # Syntax Error: for empty hashes use '{} of KeyType => ValueType'
|
||||||
{} of Int32 => Int32 # {}
|
{} of Int32 => Int32 # {} : Hash(Int32, Int32)
|
||||||
Hash(Int32, Int32).new # {}
|
Hash(Int32, Int32).new # {} : Hash(Int32, Int32)
|
||||||
|
|
||||||
# Hashes can be quickly looked up by key
|
# Hashes can be quickly looked up by key
|
||||||
hash = {"color" => "green", "number" => 5}
|
hash = {"color" => "green", "number" => 5}
|
||||||
@ -179,6 +182,9 @@ hash["color"] #=> "green"
|
|||||||
hash["no_such_key"] #=> Missing hash key: "no_such_key" (KeyError)
|
hash["no_such_key"] #=> Missing hash key: "no_such_key" (KeyError)
|
||||||
hash["no_such_key"]? #=> nil
|
hash["no_such_key"]? #=> nil
|
||||||
|
|
||||||
|
# The type of the returned value is based on all key types
|
||||||
|
hash["number"] #=> 5 : (Int32 | String)
|
||||||
|
|
||||||
# Check existence of keys hash
|
# Check existence of keys hash
|
||||||
hash.has_key? "color" #=> true
|
hash.has_key? "color" #=> true
|
||||||
|
|
||||||
@ -220,7 +226,7 @@ Range.new(1, 10).class #=> Range(Int32, Int32)
|
|||||||
# Access tuple's value by its index
|
# Access tuple's value by its index
|
||||||
tuple = {:key1, :key2}
|
tuple = {:key1, :key2}
|
||||||
tuple[1] #=> :key2
|
tuple[1] #=> :key2
|
||||||
tuple[2] #=> syntax error : Index out of bound
|
tuple[2] #=> Error: index out of bounds for Tuple(Symbol, Symbol) (2 not in -2..1)
|
||||||
|
|
||||||
# Can be expanded into multiple variables
|
# Can be expanded into multiple variables
|
||||||
a, b, c = {:a, 'b', "c"}
|
a, b, c = {:a, 'b', "c"}
|
||||||
@ -314,7 +320,7 @@ if a < 3
|
|||||||
else
|
else
|
||||||
a = true
|
a = true
|
||||||
end
|
end
|
||||||
typeof a #=> (Bool | String)
|
typeof(a) #=> (Bool | String)
|
||||||
|
|
||||||
if a && b
|
if a && b
|
||||||
# here both a and b are guaranteed not to be Nil
|
# here both a and b are guaranteed not to be Nil
|
||||||
@ -388,15 +394,19 @@ dinner #=> "quesadilla"
|
|||||||
5.even? # false
|
5.even? # false
|
||||||
5.odd? # true
|
5.odd? # true
|
||||||
|
|
||||||
# And if a method ends with an exclamation mark, it does something destructive
|
# Also by convention, if a method ends with an exclamation mark, it does
|
||||||
# like mutate the receiver. Some methods have a ! version to make a change, and
|
# something destructive like mutate the receiver.
|
||||||
|
# Some methods have a ! version to make a change, and
|
||||||
# a non-! version to just return a new changed version
|
# a non-! version to just return a new changed version
|
||||||
company_name = "Dunder Mifflin"
|
fruits = ["grapes", "apples", "bananas"]
|
||||||
company_name.gsub "Dunder", "Donald" #=> "Donald Mifflin"
|
fruits.sort #=> ["apples", "bananas", "grapes"]
|
||||||
company_name #=> "Dunder Mifflin"
|
fruits #=> ["grapes", "apples", "bananas"]
|
||||||
company_name.gsub! "Dunder", "Donald"
|
fruits.sort! #=> ["apples", "bananas", "grapes"]
|
||||||
company_name #=> "Donald Mifflin"
|
fruits #=> ["apples", "bananas", "grapes"]
|
||||||
|
|
||||||
|
# However, some mutating methods do not end in !
|
||||||
|
fruits.shift #=> "apples"
|
||||||
|
fruits #=> ["bananas", "grapes"]
|
||||||
|
|
||||||
# Define a class with the class keyword
|
# Define a class with the class keyword
|
||||||
class Human
|
class Human
|
||||||
@ -404,7 +414,7 @@ class Human
|
|||||||
# A class variable. It is shared by all instances of this class.
|
# A class variable. It is shared by all instances of this class.
|
||||||
@@species = "H. sapiens"
|
@@species = "H. sapiens"
|
||||||
|
|
||||||
# type of name is String
|
# An instance variable. Type of name is String
|
||||||
@name : String
|
@name : String
|
||||||
|
|
||||||
# Basic initializer
|
# Basic initializer
|
||||||
@ -469,9 +479,9 @@ class TestClass
|
|||||||
end
|
end
|
||||||
# Variables that start with a capital letter are constants
|
# Variables that start with a capital letter are constants
|
||||||
Var = "I'm a constant"
|
Var = "I'm a constant"
|
||||||
Var = "can't be updated" # Already initialized constant Var
|
Var = "can't be updated" # Error: already initialized constant Var
|
||||||
|
|
||||||
# Class is also an object in crystal. So class can have instance variables.
|
# Class is also an object in Crystal. So a class can have instance variables.
|
||||||
# Class variable is shared among the class and all of its descendants.
|
# Class variable is shared among the class and all of its descendants.
|
||||||
|
|
||||||
# base class
|
# base class
|
||||||
|
Loading…
Reference in New Issue
Block a user