@@ -635,10 +635,21 @@ def ==(other)
635
635
@row == other
636
636
end
637
637
638
+ # :call-seq:
639
+ # row.to_h -> hash
638
640
#
639
- # Collapses the row into a simple Hash. Be warned that this discards field
640
- # order and clobbers duplicate fields.
641
+ # Returns the new \Hash formed by adding each header-value pair in +self+
642
+ # as a key-value pair in the \Hash.
643
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
644
+ # table = CSV.parse(source, headers: true)
645
+ # row = table[0]
646
+ # row.to_h # => {"Name"=>"foo", "Value"=>"0"}
641
647
#
648
+ # Header order is preserved, but repeated headers are ignored:
649
+ # source = "Name,Name,Name\nFoo,Bar,Baz\n"
650
+ # table = CSV.parse(source, headers: true)
651
+ # row = table[0]
652
+ # row.to_h # => {"Name"=>"Foo"}
642
653
def to_h
643
654
hash = { }
644
655
each do |key , _value |
@@ -650,20 +661,35 @@ def to_h
650
661
651
662
alias_method :to_ary , :to_a
652
663
664
+ # :call-seq:
665
+ # row.to_csv -> csv_string
653
666
#
654
- # Returns the row as a CSV String. Headers are not used. Equivalent to:
655
- #
656
- # csv_row.fields.to_csv( options )
657
- #
667
+ # Returns the row as a \CSV String. Headers are not included:
668
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
669
+ # table = CSV.parse(source, headers: true)
670
+ # row = table[0]
671
+ # row.to_csv # => "foo,0\n"
658
672
def to_csv ( **options )
659
673
fields . to_csv ( **options )
660
674
end
661
675
alias_method :to_s , :to_csv
662
676
677
+ # :call-seq:
678
+ # row.dig(index_or_header, *identifiers) -> object
679
+ #
680
+ # Finds and returns the object in nested object that is specified
681
+ # by +index_or_header+ and +specifiers+.
663
682
#
664
- # Extracts the nested value specified by the sequence of +index+ or +header+ objects by calling dig at each step,
665
- # returning nil if any intermediate step is nil .
683
+ # The nested objects may be instances of various classes.
684
+ # See {Dig Methods}[https://docs.ruby-lang.org/en/master/doc/dig_methods_rdoc.html] .
666
685
#
686
+ # Examples:
687
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
688
+ # table = CSV.parse(source, headers: true)
689
+ # row = table[0]
690
+ # row.dig(1) # => "0"
691
+ # row.dig('Value') # => "0"
692
+ # row.dig(5) # => nil
667
693
def dig ( index_or_header , *indexes )
668
694
value = field ( index_or_header )
669
695
if value . nil?
@@ -678,9 +704,17 @@ def dig(index_or_header, *indexes)
678
704
end
679
705
end
680
706
707
+ # :call-seq:
708
+ # row.inspect -> string
681
709
#
682
- # A summary of fields, by header, in an ASCII compatible String.
683
- #
710
+ # Returns an ASCII-compatible \String showing:
711
+ # - Class \CSV::Row.
712
+ # - Header-value pairs.
713
+ # Example:
714
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
715
+ # table = CSV.parse(source, headers: true)
716
+ # row = table[0]
717
+ # row.inspect # => "#<CSV::Row \"Name\":\"foo\" \"Value\":\"0\">"
684
718
def inspect
685
719
str = [ "#<" , self . class . to_s ]
686
720
each do |header , field |
0 commit comments