⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.13
Server IP:
109.199.105.153
Server:
Linux connect.inboxifs.com 5.15.0-152-generic #162-Ubuntu SMP Wed Jul 23 09:48:42 UTC 2025 x86_64
Server Software:
Apache
PHP Version:
8.2.29
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
usr
/
share
/
ri
/
3.0.0
/
system
/
View File Name :
page-dig_methods_rdoc.ri
U:RDoc::TopLevel[ i I"dig_methods.rdoc:EFcRDoc::Parser::Simpleo:RDoc::Markup::Document:@parts[(S:RDoc::Markup::Heading: leveli: textI"Dig Methods;To:RDoc::Markup::BlankLine o:RDoc::Markup::Paragraph;[I"JRuby's +dig+ methods are useful for accessing nested data structures.;T@ o; ;[I"Consider this data:;To:RDoc::Markup::Verbatim;[I"item = { ;TI" id: "0001", ;TI" type: "donut", ;TI" name: "Cake", ;TI" ppu: 0.55, ;TI" batters: { ;TI" batter: [ ;TI"* {id: "1001", type: "Regular"}, ;TI", {id: "1002", type: "Chocolate"}, ;TI", {id: "1003", type: "Blueberry"}, ;TI". {id: "1004", type: "Devil's Food"} ;TI" ] ;TI" }, ;TI" topping: [ ;TI"% {id: "5001", type: "None"}, ;TI"' {id: "5002", type: "Glazed"}, ;TI"& {id: "5005", type: "Sugar"}, ;TI"/ {id: "5007", type: "Powdered Sugar"}, ;TI"9 {id: "5006", type: "Chocolate with Sprinkles"}, ;TI"* {id: "5003", type: "Chocolate"}, ;TI"% {id: "5004", type: "Maple"} ;TI" ] ;TI"} ;T:@format0o; ;[I"+Without a +dig+ method, you can write:;To;;[I"8item[:batters][:batter][1][:type] # => "Chocolate" ;T;0o; ;[I"(With a +dig+ method, you can write:;To;;[I"<item.dig(:batters, :batter, 1, :type) # => "Chocolate" ;T;0o; ;[I"8Without a +dig+ method, you can write, erroneously ;TI"N(raises <tt>NoMethodError (undefined method `[]' for nil:NilClass)</tt>):;To;;[I"'item[:batters][:BATTER][1][:type] ;T;0o; ;[I"XWith a +dig+ method, you can write (still erroneously, but avoiding the exception):;To;;[I"4item.dig(:batters, :BATTER, 1, :type) # => nil ;T;0S; ; i;I"Why Is +dig+ Better?;T@ o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0;[o; ;[I"6It has fewer syntactical elements (to get wrong).;To;;0;[o; ;[I"It reads better.;To;;0;[o; ;[I"<It does not raise an exception if an item is not found.;T@ S; ; i;I"How Does +dig+ Work?;T@ o; ;[I"The call sequence is:;To;;[I"obj.dig(*identifiers) ;T;0o; ;[I"GThe +identifiers+ define a "path" into the nested data structures:;To;;;;[ o;;0;[o; ;[I"LFor each identifier in +identifiers+, calls method \#dig on a receiver ;TI"with that identifier.;To;;0;[o; ;[I""The first receiver is +self+.;To;;0;[o; ;[I"REach successive receiver is the value returned by the previous call to +dig+.;To;;0;[o; ;[I"PThe value finally returned is the value returned by the last call to +dig+.;T@ o; ;[I"RA +dig+ method raises an exception if any receiver does not respond to \#dig:;To;;[I"h = { foo: 1 } ;TI"=# Raises TypeError (Integer does not have #dig method): ;TI"h.dig(:foo, :bar) ;T;0S; ; i;I"What Else?;T@ o; ;[I"?The structure above has \Hash objects and \Array objects, ;TI".both of which have instance method +dig+.;T@ o; ;[I"LAltogether there are six built-in Ruby classes that have method +dig+, ;TI"Athree in the core classes and three in the standard library.;T@ o; ;[I"In the core:;To;;;;[o;;0;[o; ;[I"8Array#dig: the first argument is an \Integer index.;To;;0;[o; ;[I"+Hash#dig: the first argument is a key.;To;;0;[o; ;[I"-Struct#dig: the first argument is a key.;T@ o; ;[I"In the standard library:;To;;;;[o;;0;[o; ;[I":OpenStruct#dig: the first argument is a \String name.;To;;0;[o; ;[I"QCSV::Table#dig: the first argument is an \Integer index or a \String header.;To;;0;[o; ;[I"OCSV::Row#dig: the first argument is an \Integer index or a \String header.;T: @file@:0@omit_headings_from_table_of_contents_below0