Project

General

Profile

Actions

Feature #11688

closed

Struct and OpenStruct will be able to chain the Hash#dig Array#dig

Added by kachick (Kenichi Kamiya) over 8 years ago. Updated over 8 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-dev:49344]

Description

Struct and OpenStruct are rejected with #dig? I couldn't get from [Feature #11643]

patch


Files

bm_dig.rb (1.88 KB) bm_dig.rb nobu (Nobuyoshi Nakada), 11/16/2015 09:40 AM

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

  • Status changed from Open to Feedback

As those two classes were not parts of [Feature #11643], they are not rejected nor discussed.
Struct#dig should be at least, IMHO.

patch

I feel that an argument of OpenStruct#dig should not raise an exception until it is used, as well as Array and Hash.

[].dig("foo")          #=> no implicit conversion of String into Integer (TypeError)
[].dig(1, "foo")       #=> nil
OpenStruct.new.dig(0)  #=> undefined method `to_sym' for 0:Fixnum (NoMethodError)

It may be too strict though.
How do you think?

Actions #2

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

  • Status changed from Feedback to Closed

Applied in changeset r52596.


struct.c: dig

  • object.c (rb_obj_dig): dig in nested structs too.
  • struct.c (rb_struct_dig): new method Struct#dig.
    [Feature #11688]

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

I benchmarked with the attached script.

                     |     user |   system |    total |      real

-------------------------|---------:|---------:|---------:|---------:
Hash only(C) | 0.030000 | 0.000000 | 0.030000 | 0.026955
Hash only(Ruby) | 0.810000 | 0.040000 | 0.850000 | 0.873564
Hash+Array (C) | 0.040000 | 0.010000 | 0.050000 | 0.048672
Hash+Array (Ruby) | 1.670000 | 0.060000 | 1.730000 | 1.777769
OpenStruct (C) | 0.940000 | 0.040000 | 0.980000 | 1.001595
OpenStruct (Ruby) | 0.930000 | 0.040000 | 0.970000 | 0.989792
OpenStruct+Array (C) | 1.040000 | 0.030000 | 1.070000 | 1.093703
OpenStruct+Array (Ruby) | 1.840000 | 0.030000 | 1.870000 | 1.882345
Mix Hash (C) | 0.030000 | 0.000000 | 0.030000 | 0.034446
Mix Hash (RUBY) | 1.770000 | 0.040000 | 1.810000 | 1.826980
Mix OpenStruct (C) | 1.470000 | 0.030000 | 1.500000 | 1.516417
Mix OpenStruct (Ruby) | 1.920000 | 0.050000 | 1.970000 | 1.987407

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

Matz has approved both, last afternoon (JST).

Actions #5

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

  • Status changed from Open to Closed

Applied in changeset r52611.


OpenStruct#dig

  • lib/ostruct.rb (dig): Implement OpenStruct#dig
    [Feature #11688]

Updated by kachick (Kenichi Kamiya) over 8 years ago

Nobuyoshi Nakada wrote:

As those two classes were not parts of [Feature #11643], they are not rejected nor discussed.
Struct#dig should be at least, IMHO.

patch

I feel that an argument of OpenStruct#dig should not raise an exception until it is used, as well as Array and Hash.

[].dig("foo")          #=> no implicit conversion of String into Integer (TypeError)
[].dig(1, "foo")       #=> nil
OpenStruct.new.dig(0)  #=> undefined method `to_sym' for 0:Fixnum (NoMethodError)

It may be too strict though.
How do you think?

Sorry for the late reply!

nobu

Thanks for your improvemets!
And I'm sorry to bother you, I forgot to adjust arity as https://github.com/ruby/ruby/commit/482530680c18ea2e44c3300c6f323fabc3bd55f7#diff-f0ac5119f74bfda273347c9925e910d7R226

It may be too strict though.
How do you think?

At first, I felt its better to raise an error as Array#dig given a non index and OpenStruct#dig given a non method name.
But since I read https://bugs.ruby-lang.org/issues/11762, I think returning nil is reasonable when the nested receiver respond to #dig.

Matz

Thanks to contain both #dig into Ruby 2.3, I'll enjoy!

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0