Feature #11688
closedStruct and OpenStruct will be able to chain the Hash#dig Array#dig
Description
Files
Updated by nobu (Nobuyoshi Nakada) about 9 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.
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?
Updated by nobu (Nobuyoshi Nakada) about 9 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) about 9 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) about 9 years ago
Matz has approved both, last afternoon (JST).
Updated by nobu (Nobuyoshi Nakada) about 9 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) about 9 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.I feel that an argument of
OpenStruct#dig
should not raise an exception until it is used, as well asArray
andHash
.[].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!