8000 Adds CSV::Row#fetch and CSV::Row#has_key? methods by dball · Pull Request #225 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

Adds CSV::Row#fetch and CSV::Row#has_key? methods #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
35 changes: 34 additions & 1 deletion lib/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def headers
# field( header, offset )
# field( index )
#
# This method will fetch the field value by +header+ or +index+. If a field
# This method will return the field value by +header+ or +index+. If a field
# is not found, +nil+ is returned.
#
# When provided, +offset+ ensures that a header match occurrs on or later
Expand All @@ -291,6 +291,39 @@ def field(header_or_index, minimum_index = 0)
end
alias_method :[], :field

#
# :call-seq:
# fetch( header )
# fetch( header ) { |row| ... }
# fetch( header, default )
#
# This method will fetch the field value by +header+. It has the same
# behavior as Hash#fetch: if there is a field with the given +header+, its
# value is returned. Otherwise, if a block is given, it is yielded the
# +header+ and its result is returned; if a +default+ is given as the
# second argument, it is returned; otherwise a KeyError is raised.
#
def fetch(header, *varargs)
raise ArgumentError, "Too many arguments" if varargs.length > 1
pair = @row.assoc(header)
if pair
pair.last
else
if block_given?
yield header
elsif varargs.empty?
raise KeyError, "key not found: #{header}"
else
varargs.first
end
end
end

# Returns +true+ if there is a field with the given +header+.
def has_key?(header)
!!@row.assoc(header)
end

#
# :call-seq:
# []=( header, value )
Expand Down
27 changes: 27 additions & 0 deletions test/csv/test_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,33 @@ def test_field
assert_equal(nil, @row.field("A", 5))
end

def test_fetch
# only by name
assert_equal(2, @row.fetch('B'))

# missing header raises KeyError
assert_raise KeyError do
@row.fetch('foo')
end

# missing header yields itself to block
assert_equal 'bar', @row.fetch('foo') { |header|
header == 'foo' ? 'bar' : false }

# missing header returns the given default value
assert_equal 'bar', @row.fetch('foo', 'bar')

# more than one vararg raises ArgumentError
assert_raise ArgumentError do
@row.fetch('foo', 'bar', 'baz')
end
end

def test_has_key?
assert_equal(true, @row.has_key?('B'))
assert_equal(false, @row.has_key?('foo'))
end

def test_set_field
# set field by name
assert_equal(100, @row["A"] = 100)
Expand Down
0