Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jul 18, 2021
1 parent 32068db commit 408b371
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 3 deletions.
9 changes: 9 additions & 0 deletions crystal/examples/code_executed.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
puts "Hello Crystal!"

class Hello
puts "Hello from class"
end

struct My
puts "Hello from struct"
end
4 changes: 4 additions & 0 deletions crystal/examples/intro/gets_number.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
print "Give me a number! "
number = gets
puts number
puts number + 1
4 changes: 2 additions & 2 deletions crystal/examples/other/gets_not_nil.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
x = gets
puts x.nil?
# x = gets
# puts x.nil?

x = gets.not_nil!
puts x.nil?
12 changes: 12 additions & 0 deletions crystal/examples/restrict_type_in_code.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if Random.rand < 0.5
x = "abc"
else
x = nil
end

if x.nil?
puts "nil"
else
puts x
puts x.size
end
20 changes: 20 additions & 0 deletions crystal/examples/restrict_type_in_code_for_hash_broken.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
if Random.rand < 0.5
prs = {
"name" => "Foo",
"title" => "Manager",
}
else
prs = {
"name" => "Foo",
"title" => nil,
}
end

p! prs

if prs["title"].nil?
puts "nil"
else
puts "not nil"
puts prs["title"].size
end
28 changes: 28 additions & 0 deletions crystal/examples/restrict_type_in_code_for_hash_fixed.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
if Random.rand < 0.5
prs = {
"name" => "Foo",
"title" => "Manager",
}
else
prs = {
"name" => "Foo",
"title" => nil,
}
end

p! prs

title = prs["title"]
if title.nil?
puts "nil"
else
puts "not nil"
puts title.size
end

if title = prs["title"]
puts "not nil"
puts title.size
else
puts "nil"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
struct Person
getter first : String
getter family : String?

def initialize(@first)
end

def initialize(@first, @family)
end
end

if Random.rand < 0.5
prs = Person.new("Foo")
else
prs = Person.new("Foo", "Bar")
end

if prs.family.nil?
puts "nil"
else
puts "not nil"
puts prs.family.size
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
struct Person
getter first : String
getter family : String?

def initialize(@first)
end

def initialize(@first, @family)
end
end

if Random.rand < 0.5
prs = Person.new("Foo")
else
prs = Person.new("Foo", "Bar")
end

family = prs.family
if family.nil?
puts "nil"
else
puts "not nil"
puts family.size
end

if family = prs.family
puts "not nil"
puts family.size
else
puts "nil"
end
1 change: 0 additions & 1 deletion crystal/examples/struct/struct_from_json_upper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ require "json"
struct Person
include JSON::Serializable


getter name : String

@[JSON::Field(key: "Email")]
Expand Down
3 changes: 3 additions & 0 deletions crystal/examples/struct/struct_from_json_upper.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
prs # => Person(@name="Bar", @email="[email protected]")
prs.name # => "Bar"
prs.email # => "[email protected]"
47 changes: 47 additions & 0 deletions crystal/examples/type_inference_from_struct.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
struct Dog
getter name : String
getter owner : String

def initialize(@name, @owner)
end
end

struct Cat
getter name : String
getter staff : String

def initialize(@name, @staff)
end
end

if Random.rand < 0.5
animal = Dog.new("Gin", "Foo")
else
animal = Cat.new("Tonic", "Bar")
end

p! animal
p! animal.class

# if animal.class == Cat
# p! animal.staff
# else
# p! animal.owner
# end

if animal.is_a?(Cat)
p! animal.staff
else
p! animal.owner
end

case animal
when Cat
puts "cat"
p! animal.staff
when Dog
puts "dog"
p! animal.owner
else
puts "other"
end
1 change: 1 addition & 0 deletions crystal/structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ when you leave the function.
{id: struct-from-json-upper-case}

* We cannot have attributes starting with upper case so we have to convert the field names to lowercase:
* Using [attribute annotation](https://crystal-lang.org/reference/syntax_and_semantics/annotations/index.html)

![](examples/struct/struct_from_json_upper.cr)
![](examples/struct/struct_from_json_upper.out)
Expand Down

0 comments on commit 408b371

Please sign in to comment.