Skip to content

Commit 8d749df

Browse files
BurdetteLamarkou
authored andcommitted
[ruby/csv] Enhanced RDoc for CSV::Row (ruby#173)
ruby/csv@99956c671d
1 parent 02a6d74 commit 8d749df

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

lib/csv/row.rb

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,21 @@ def ==(other)
524524
@row == other
525525
end
526526

527+
# :call-seq:
528+
# row.to_h -> hash
527529
#
528-
# Collapses the row into a simple Hash. Be warned that this discards field
529-
# order and clobbers duplicate fields.
530+
# Returns the new \Hash formed by adding each header-value pair in +self+
531+
# as a key-value pair in the \Hash.
532+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
533+
# table = CSV.parse(source, headers: true)
534+
# row = table[0]
535+
# row.to_h # => {"Name"=>"foo", "Value"=>"0"}
530536
#
537+
# Header order is preserved, but repeated headers are ignored:
538+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
539+
# table = CSV.parse(source, headers: true)
540+
# row = table[0]
541+
# row.to_h # => {"Name"=>"Foo"}
531542
def to_h
532543
hash = {}
533544
each do |key, _value|
@@ -539,20 +550,35 @@ def to_h
539550

540551
alias_method :to_ary, :to_a
541552

553+
# :call-seq:
554+
# row.to_csv -> csv_string
542555
#
543-
# Returns the row as a CSV String. Headers are not used. Equivalent to:
544-
#
545-
# csv_row.fields.to_csv( options )
546-
#
556+
# Returns the row as a \CSV String. Headers are not included:
557+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
558+
# table = CSV.parse(source, headers: true)
559+
# row = table[0]
560+
# row.to_csv # => "foo,0\n"
547561
def to_csv(**options)
548562
fields.to_csv(**options)
549563
end
550564
alias_method :to_s, :to_csv
551565

566+
# :call-seq:
567+
# row.dig(index_or_header, *identifiers) -> object
568+
#
569+
# Finds and returns the object in nested object that is specified
570+
# by +index_or_header+ and +specifiers+.
552571
#
553-
# Extracts the nested value specified by the sequence of +index+ or +header+ objects by calling dig at each step,
554-
# returning nil if any intermediate step is nil.
572+
# The nested objects may be instances of various classes.
573+
# See {Dig Methods}[https://docs.ruby-lang.org/en/master/doc/dig_methods_rdoc.html].
555574
#
575+
# Examples:
576+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
577+
# table = CSV.parse(source, headers: true)
578+
# row = table[0]
579+
# row.dig(1) # => "0"
580+
# row.dig('Value') # => "0"
581+
# row.dig(5) # => nil
556582
def dig(index_or_header, *indexes)
557583
value = field(index_or_header)
558584
if value.nil?
@@ -567,9 +593,17 @@ def dig(index_or_header, *indexes)
567593
end
568594
end
569595

596+
# :call-seq:
597+
# row.inspect -> string
570598
#
571-
# A summary of fields, by header, in an ASCII compatible String.
572-
#
599+
# Returns an ASCII-compatible \String showing:
600+
# - Class \CSV::Row.
601+
# - Header-value pairs.
602+
# Example:
603+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
604+
# table = CSV.parse(source, headers: true)
605+
# row = table[0]
606+
# row.inspect # => "#<CSV::Row \"Name\":\"foo\" \"Value\":\"0\">"
573607
def inspect
574608
str = ["#<", self.class.to_s]
575609
each do |header, field|

0 commit comments

Comments
 (0)