* lib/csv.rb: Added more Hash methods to CSV::Row.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38633 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7b34c2f81a
commit
93030d0e4d
@ -1,3 +1,7 @@
|
|||||||
|
Tue Dec 28 01:13:48 2012 James Edward Gray II <james@graysoftinc.com>
|
||||||
|
|
||||||
|
* lib/csv.rb: Added more Hash methods to CSV::Row.
|
||||||
|
|
||||||
Thu Dec 27 23:27:15 2012 Keiju Ishitsuka <keiju@ishitsuka.com>
|
Thu Dec 27 23:27:15 2012 Keiju Ishitsuka <keiju@ishitsuka.com>
|
||||||
|
|
||||||
* lib/irb/ruby-lex.rb: make lex_state to EXPR_END when next token
|
* lib/irb/ruby-lex.rb: make lex_state to EXPR_END when next token
|
||||||
|
38
lib/csv.rb
38
lib/csv.rb
@ -274,7 +274,7 @@ class CSV
|
|||||||
# field( header, offset )
|
# field( header, offset )
|
||||||
# field( index )
|
# 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.
|
# is not found, +nil+ is returned.
|
||||||
#
|
#
|
||||||
# When provided, +offset+ ensures that a header match occurrs on or later
|
# When provided, +offset+ ensures that a header match occurrs on or later
|
||||||
@ -291,6 +291,42 @@ class CSV
|
|||||||
end
|
end
|
||||||
alias_method :[], :field
|
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
|
||||||
|
alias_method :include?, :has_key?
|
||||||
|
alias_method :key?, :has_key?
|
||||||
|
alias_method :member?, :has_key?
|
||||||
|
|
||||||
#
|
#
|
||||||
# :call-seq:
|
# :call-seq:
|
||||||
# []=( header, value )
|
# []=( header, value )
|
||||||
|
@ -78,6 +78,33 @@ class TestCSV::Row < TestCSV
|
|||||||
assert_equal(nil, @row.field("A", 5))
|
assert_equal(nil, @row.field("A", 5))
|
||||||
end
|
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
|
def test_set_field
|
||||||
# set field by name
|
# set field by name
|
||||||
assert_equal(100, @row["A"] = 100)
|
assert_equal(100, @row["A"] = 100)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user