Skip to content

Commit

Permalink
Add NamedTuple#has_key?(key : String) overload
Browse files Browse the repository at this point in the history
See #3824
  • Loading branch information
Sija authored and asterite committed Jan 4, 2017
1 parent 8aff8af commit 819b104
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion spec/std/named_tuple_spec.cr
Expand Up @@ -207,13 +207,20 @@ describe "NamedTuple" do
i.should eq(2)
end

it "does has_key?" do
it "does has_key? with symbol" do
tup = {a: 1, b: 'a'}
tup.has_key?(:a).should be_true
tup.has_key?(:b).should be_true
tup.has_key?(:c).should be_false
end

it "does has_key? with string" do
tup = {a: 1, b: 'a'}
tup.has_key?("a").should be_true
tup.has_key?("b").should be_true
tup.has_key?("c").should be_false
end

it "does empty" do
{a: 1}.empty?.should be_false
end
Expand Down
8 changes: 8 additions & 0 deletions src/named_tuple.cr
Expand Up @@ -219,6 +219,14 @@ struct NamedTuple
false
end

# ditto
def has_key?(key : String) : Bool
{% for key in T %}
return true if {{key.stringify}} == key
{% end %}
false
end

# Appends a string representation of this named tuple to the given `IO`.
#
# ```
Expand Down

0 comments on commit 819b104

Please sign in to comment.