<commit_before>require File.expand_path('../../../spec_helper', __FILE__)

describe "Hash#dig" do

  it "returns [] with one arg" do
    h = { 0 => false, :a => 1 }
    h.dig(:a).should == 1
    h.dig(0).should be_false
    h.dig(1).should be_nil
  end

  it "does recurse" do
    h = { foo: { bar: { baz: 1 } } }
    h.dig(:foo, :bar, :baz).should == 1
    h.dig(:foo, :baz).should be_nil
    h.dig(:bar, :baz, :foo).should be_nil
  end

  it "raises without args" do
    lambda { { the: 'borg' }.dig() }.should raise_error(ArgumentError)
  end

end<commit_msg>spec nested type-mixed dig behaviour (including raw def-d obj.dig) on Ruby 2.3<commit_after>require File.expand_path('../../../spec_helper', __FILE__)

describe "Hash#dig" do

  it "returns [] with one arg" do
    h = { 0 => false, :a => 1 }
    h.dig(:a).should == 1
    h.dig(0).should be_false
    h.dig(1).should be_nil
  end

  it "does recurse" do
    h = { foo: { bar: { baz: 1 } } }
    h.dig(:foo, :bar, :baz).should == 1
    h.dig(:foo, :baz).should be_nil
    h.dig(:bar, :baz, :foo).should be_nil
  end

  it "raises without args" do
    lambda { { the: 'borg' }.dig() }.should raise_error(ArgumentError)
  end

  it "handles type-mixed deep digging" do
    h = {}
    h[:foo] = [ { :bar => [ 1 ] }, [ obj = Object.new, 'str' ] ]
    def obj.dig(*args); [ 42 ] end

    h.dig(:foo, 0, :bar).should == [ 1 ]
    h.dig(:foo, 0, :bar, 0).should == 1
    h.dig(:foo, 0, :bar, 0, 0).should be_nil
    h.dig(:foo, 1, 1).should == 'str'
    h.dig(:foo, 1, 1, 0).should be_nil
    # MRI does not recurse values returned from `obj.dig`
    h.dig(:foo, 1, 0, 0).should == [ 42 ]
    h.dig(:foo, 1, 0, 0, 10).should == [ 42 ]
  end

end
{'fraction_non_alphanumeric': 0.19246615656268393, 'fraction_numerical': 0.025897586815773983, 'mean_word_length': 2.586497890295359, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4400739', 'n_tokens_mistral': 722, 'n_tokens_neox': 663, 'n_words': 187}