-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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? |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |
23 changes: 23 additions & 0 deletions
23
crystal/examples/restrict_type_in_code_for_struct_attribute_broken.cr
This file contains 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
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 |
31 changes: 31 additions & 0 deletions
31
crystal/examples/restrict_type_in_code_for_struct_attribute_fixed.cr
This file contains 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
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 |
This file contains 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
This file contains 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
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]" |
This file contains 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
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 |
This file contains 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