Skip to content

Commit

Permalink
Change Hash#key to Hash#key_for (#5444)
Browse files Browse the repository at this point in the history
* Change Hash#key to Hash#key_for

* Update Spec description for Hash#key_for and Hash#key_for?
  • Loading branch information
marksiemers authored and RX14 committed Jan 11, 2018
1 parent f59a349 commit f16e63a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
22 changes: 11 additions & 11 deletions spec/std/hash_spec.cr
Expand Up @@ -150,43 +150,43 @@ describe "Hash" do
end
end

describe "key" do
describe "key_for" do
it "returns the first key with the given value" do
hash = {"foo" => "bar", "baz" => "qux"}
hash.key("bar").should eq("foo")
hash.key("qux").should eq("baz")
hash.key_for("bar").should eq("foo")
hash.key_for("qux").should eq("baz")
end

it "raises when no key pairs with the given value" do
expect_raises KeyError do
{"foo" => "bar"}.key("qux")
{"foo" => "bar"}.key_for("qux")
end
end

describe "if block is given," do
it "returns the first key with the given value" do
hash = {"foo" => "bar", "baz" => "bar"}
hash.key("bar") { |value| value.upcase }.should eq("foo")
hash.key_for("bar") { |value| value.upcase }.should eq("foo")
end

it "yields the argument if no hash key pairs with the value" do
hash = {"foo" => "bar"}
hash.key("qux") { |value| value.upcase }.should eq("QUX")
hash.key_for("qux") { |value| value.upcase }.should eq("QUX")
end
end
end

describe "key?" do
describe "key_for?" do
it "returns the first key with the given value" do
hash = {"foo" => "bar", "baz" => "qux"}
hash.key?("bar").should eq("foo")
hash.key?("qux").should eq("baz")
hash.key_for?("bar").should eq("foo")
hash.key_for?("qux").should eq("baz")
end

it "returns nil if no key pairs with the given value" do
hash = {"foo" => "bar", "baz" => "qux"}
hash.key?("foobar").should eq nil
hash.key?("bazqux").should eq nil
hash.key_for?("foobar").should eq nil
hash.key_for?("bazqux").should eq nil
end
end

Expand Down
32 changes: 16 additions & 16 deletions src/hash.cr
Expand Up @@ -162,38 +162,38 @@ class Hash(K, V)
indexes.map { |index| self[index] }
end

# Returns the first key with the given *value*, else raises `KeyError`.
# Returns a key with the given *value*, else raises `KeyError`.
#
# ```
# hash = {"foo" => "bar", "baz" => "qux"}
# hash.key("bar") # => "foo"
# hash.key("qux") # => "baz"
# hash.key("foobar") # raises KeyError (Missing hash key for value: foobar)
# hash.key_for("bar") # => "foo"
# hash.key_for("qux") # => "baz"
# hash.key_for("foobar") # raises KeyError (Missing hash key for value: foobar)
# ```
def key(value)
key(value) { raise KeyError.new "Missing hash key for value: #{value}" }
def key_for(value)
key_for(value) { raise KeyError.new "Missing hash key for value: #{value}" }
end

# Returns the first key with the given *value*, else `nil`.
# Returns a key with the given *value*, else `nil`.
#
# ```
# hash = {"foo" => "bar", "baz" => "qux"}
# hash.key?("bar") # => "foo"
# hash.key?("qux") # => "baz"
# hash.key?("foobar") # => nil
# hash.key_for?("bar") # => "foo"
# hash.key_for?("qux") # => "baz"
# hash.key_for?("foobar") # => nil
# ```
def key?(value)
key(value) { nil }
def key_for?(value)
key_for(value) { nil }
end

# Returns the first key with the given *value*, else yields *value* with the given block.
# Returns a key with the given *value*, else yields *value* with the given block.
#
# ```
# hash = {"foo" => "bar"}
# hash.key("bar") { |value| value.upcase } # => "foo"
# hash.key("qux") { |value| value.upcase } # => "QUX"
# hash.key_for("bar") { |value| value.upcase } # => "foo"
# hash.key_for("qux") { |value| value.upcase } # => "QUX"
# ```
def key(value)
def key_for(value)
each do |k, v|
return k if v == value
end
Expand Down
2 changes: 1 addition & 1 deletion src/llvm/enums.cr
Expand Up @@ -134,7 +134,7 @@ module LLVM
end

def self.from_kind(kind)
@@kind_ids.key(kind)
@@kind_ids.key_for(kind)
end
end
{% else %}
Expand Down

0 comments on commit f16e63a

Please sign in to comment.