Skip to content

Commit

Permalink
Fix example for NamedTuple.from (#3911)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjago authored and Martin Verzilli committed Jan 30, 2017
1 parent bda40f8 commit 4c15dec
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/named_tuple.cr
Expand Up @@ -38,13 +38,15 @@ struct NamedTuple
end

# Creates a named tuple from the given hash, with elements casted to the given types.
# Here the Int32 | String union is cast to Int32.
#
# ```
# NamedTuple(foo: String, bar: Int64).from({:foo => "world", :bar => 2}) # => {foo: "world", bar: 2}
# NamedTuple(foo: String, bar: Int64).from({"foo" => "world", "bar" => 2}) # => {foo: "world", bar: 2}
# NamedTuple(foo: String, bar: Int64).from({:foo => "world", :bar => 2}).class # => {foo: String, bar: Int64}
# ```
# num_or_str = 42.as(Int32 | String)
# NamedTuple(name: String, val: Int32).from({"name" => "number", "val" => num_or_str}) # => {name: "number", val: 42}
#
# num_or_str = "a string".as(Int32 | String)
# NamedTuple(name: String, val: Int32).from({"name" => "number", "val" => num_or_str}) # raises TypeCastError (cast from String to Int32 failed)
# ```
# See also: `#from`.
def self.from(hash : Hash)
{% begin %}
Expand Down

0 comments on commit 4c15dec

Please sign in to comment.