Skip to content

Commit 4c15dec

Browse files
mjagoMartin Verzilli
authored and
Martin Verzilli
committedJan 30, 2017
Fix example for NamedTuple.from (#3911)
1 parent bda40f8 commit 4c15dec

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed
 

‎src/named_tuple.cr

+6-4
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ struct NamedTuple
3838
end
3939

4040
# Creates a named tuple from the given hash, with elements casted to the given types.
41+
# Here the Int32 | String union is cast to Int32.
4142
#
4243
# ```
43-
# NamedTuple(foo: String, bar: Int64).from({:foo => "world", :bar => 2}) # => {foo: "world", bar: 2}
44-
# NamedTuple(foo: String, bar: Int64).from({"foo" => "world", "bar" => 2}) # => {foo: "world", bar: 2}
45-
# NamedTuple(foo: String, bar: Int64).from({:foo => "world", :bar => 2}).class # => {foo: String, bar: Int64}
46-
# ```
44+
# num_or_str = 42.as(Int32 | String)
45+
# NamedTuple(name: String, val: Int32).from({"name" => "number", "val" => num_or_str}) # => {name: "number", val: 42}
4746
#
47+
# num_or_str = "a string".as(Int32 | String)
48+
# NamedTuple(name: String, val: Int32).from({"name" => "number", "val" => num_or_str}) # raises TypeCastError (cast from String to Int32 failed)
49+
# ```
4850
# See also: `#from`.
4951
def self.from(hash : Hash)
5052
{% begin %}

0 commit comments

Comments
 (0)