Skip to content

Commit 99956c6

Browse files
Enhanced RDoc for CSV::Row (#173)
1 parent d91ae93 commit 99956c6

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
@@ -635,10 +635,21 @@ def ==(other)
635635
@row == other
636636
end
637637

638+
# :call-seq:
639+
# row.to_h -> hash
638640
#
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"}
641647
#
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"}
642653
def to_h
643654
hash = {}
644655
each do |key, _value|
@@ -650,20 +661,35 @@ def to_h
650661

651662
alias_method :to_ary, :to_a
652663

664+
# :call-seq:
665+
# row.to_csv -> csv_string
653666
#
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"
658672
def to_csv(**options)
659673
fields.to_csv(**options)
660674
end
661675
alias_method :to_s, :to_csv
662676

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+.
663682
#
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].
666685
#
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
667693
def dig(index_or_header, *indexes)
668694
value = field(index_or_header)
669695
if value.nil?
@@ -678,9 +704,17 @@ def dig(index_or_header, *indexes)
678704
end
679705
end
680706

707+
# :call-seq:
708+
# row.inspect -> string
681709
#
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\">"
684718
def inspect
685719
str = ["#<", self.class.to_s]
686720
each do |header, field|

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy