From 8290af0b6db99352410052372775bae0da7db0a9 Mon Sep 17 00:00:00 2001 From: Gabor Szabo Date: Tue, 8 Jun 2021 14:48:38 +0300 Subject: [PATCH] update --- crystal/examples/arrays/filter.cr | 1 - crystal/examples/classes/class_with_property.cr | 2 +- crystal/examples/functions/default_value.cr | 8 ++++---- crystal/examples/functions/parameter_passing.cr | 6 +++--- crystal/examples/json_to_named_tuple.cr | 11 +++++++++++ crystal/examples/try.cr | 4 ++++ crystal/other.md | 6 ++++++ 7 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 crystal/examples/json_to_named_tuple.cr diff --git a/crystal/examples/arrays/filter.cr b/crystal/examples/arrays/filter.cr index 6b8de72c9..2d37f65a8 100644 --- a/crystal/examples/arrays/filter.cr +++ b/crystal/examples/arrays/filter.cr @@ -5,4 +5,3 @@ puts small big = numbers.reject! do |num| num < 10 end puts big - \ No newline at end of file diff --git a/crystal/examples/classes/class_with_property.cr b/crystal/examples/classes/class_with_property.cr index 2eab3a0b0..be78f758d 100644 --- a/crystal/examples/classes/class_with_property.cr +++ b/crystal/examples/classes/class_with_property.cr @@ -1,5 +1,5 @@ class Person - property name : String + property name : String def initialize(@name = "George") end diff --git a/crystal/examples/functions/default_value.cr b/crystal/examples/functions/default_value.cr index ac25d42ee..159023542 100644 --- a/crystal/examples/functions/default_value.cr +++ b/crystal/examples/functions/default_value.cr @@ -1,6 +1,6 @@ def welcome(name = "World") return "Hello #{name}!" - end - - puts welcome - puts welcome("Foo") \ No newline at end of file +end + +puts welcome +puts welcome("Foo") diff --git a/crystal/examples/functions/parameter_passing.cr b/crystal/examples/functions/parameter_passing.cr index 32992acae..706ecd1ea 100644 --- a/crystal/examples/functions/parameter_passing.cr +++ b/crystal/examples/functions/parameter_passing.cr @@ -1,5 +1,5 @@ def welcome(name) return "Hello #{name}!" - end - - puts welcome("Foo") \ No newline at end of file +end + +puts welcome("Foo") diff --git a/crystal/examples/json_to_named_tuple.cr b/crystal/examples/json_to_named_tuple.cr new file mode 100644 index 000000000..1e8e9f33e --- /dev/null +++ b/crystal/examples/json_to_named_tuple.cr @@ -0,0 +1,11 @@ +require "json" + + +alias Thing = NamedTuple(name: String, number: Int32) + +thing_json = %{{"name": "table", "number": 3}} +puts thing_json + +tg = Thing.from_json(thing_json) +puts tg + diff --git a/crystal/examples/try.cr b/crystal/examples/try.cr index 4d282a4e5..3cb696a80 100644 --- a/crystal/examples/try.cr +++ b/crystal/examples/try.cr @@ -1,4 +1,8 @@ +# counter = Int32|Nil + +# counter = 2 +# puts counter + 1 # person = { diff --git a/crystal/other.md b/crystal/other.md index da0de0cf7..4289bafa5 100644 --- a/crystal/other.md +++ b/crystal/other.md @@ -203,8 +203,14 @@ crystal spec ![](examples/gravatar.cr) +## JSON to NamedTuple +{id: json-to-namedtuple} +{i: NamedTuple} +{i: from_json} + ## Reading a JSON file {id: reading-a-json-file} +{i: NamedTuple} ![](examples/math.json) ![](examples/read_math_json.cr)