ruby-changes:26582
From: jeg2 <ko1@a...>
Date: Fri, 28 Dec 2012 01:16:03 +0900 (JST)
Subject: [ruby-changes:26582] jeg2:r38633 (trunk): * lib/csv.rb: Added more Hash methods to CSV::Row.
jeg2 2012-12-28 01:15:53 +0900 (Fri, 28 Dec 2012) New Revision: 38633 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38633 Log: * lib/csv.rb: Added more Hash methods to CSV::Row. Modified files: trunk/ChangeLog trunk/lib/csv.rb trunk/test/csv/test_row.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 38632) +++ ChangeLog (revision 38633) @@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Dec 28 01:13:48 2012 James Edward Gray II <james@g...> + + * lib/csv.rb: Added more Hash methods to CSV::Row. + Thu Dec 27 23:27:15 2012 Keiju Ishitsuka <keiju@i...> * lib/irb/ruby-lex.rb: make lex_state to EXPR_END when next token @@ -2924,7 +2928,7 @@ Thu Nov 29 15:10:45 2012 NARUSE, Yui < https://github.com/ruby/ruby/blob/trunk/ChangeLog#L2928 * test/minitest/test_minitest_unit.rb: restore orig_verbose only if it is set. This broke rdoc's tests. - http://u64.rubyci.org/~chkbuild/ruby-trunk/log/20121129T050102Z.diff.html.gz + http://u64.rubyci.org/~chkbuild/ruby-trunk/log/20121129T050102Z.diff.html.gz Thu Nov 29 14:56:30 2012 Koichi Sasada <ko1@a...> @@ -6986,7 +6990,7 @@ Fri Sep 28 17:26:27 2012 NARUSE, Yui < https://github.com/ruby/ruby/blob/trunk/ChangeLog#L6990 fails on Windows with msys bash. It makes comparing paths case-insensitive. pick from upstream to fix a failure of test-all [ruby-core:47711] - https://github.com/rubygems/rubygems/commit/c474edb2f3704206f04da1c8c6cf9fb079d84abe + https://github.com/rubygems/rubygems/commit/c474edb2f3704206f04da1c8c6cf9fb079d84abe Fri Sep 28 15:44:45 2012 Shugo Maeda <shugo@r...> @@ -8864,7 +8868,7 @@ Sun Jul 22 23:58:48 2012 NARUSE, Yui < https://github.com/ruby/ruby/blob/trunk/ChangeLog#L8868 Sat Jul 21 06:21:45 2012 NARUSE, Yui <naruse@r...> * lib/net/http.rb: fixes for r36476. [Feature #6546] - http://u64.rubyci.org/~chkbuild/ruby-trunk/log/20120720T030101Z.diff.html.gz + http://u64.rubyci.org/~chkbuild/ruby-trunk/log/20120720T030101Z.diff.html.gz * lib/net/http.rb (Net::HTTP.newobj): return back for compatibility. @@ -10516,7 +10520,7 @@ Wed Jun 13 10:20:27 2012 Nobuyoshi Naka https://github.com/ruby/ruby/blob/trunk/ChangeLog#L10520 * process.c (rb_exec_fillarg): use shell if the first word is reserved or special built-in name. - http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html + http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html * process.c (rb_exec_fillarg): treat '=' only in the first word. if the first word does not contain '=', it is the command name and @@ -10669,7 +10673,7 @@ Mon Jun 11 06:17:06 2012 NARUSE, Yui < https://github.com/ruby/ruby/blob/trunk/ChangeLog#L10673 * thread_pthread.c (native_cond_initialize): fix typo in r36022. this cause a failure on FreeBSD 8.2 amd64. - http://fbsd.rubyci.org/~chkbuild/ruby-trunk/log/20120610T130201Z.diff.html.gz + http://fbsd.rubyci.org/~chkbuild/ruby-trunk/log/20120610T130201Z.diff.html.gz Mon Jun 11 05:21:57 2012 Koichi Sasada <ko1@a...> @@ -10748,7 +10752,7 @@ Sun Jun 10 15:49:47 2012 Tanaka Akira https://github.com/ruby/ruby/blob/trunk/ChangeLog#L10752 Sun Jun 10 14:19:33 2012 NARUSE, Yui <naruse@r...> * configure.in: NetBSD 6 adds libexecinfo but it only works on amd64. - http://www.mail-archive.com/source-changes-full@n.../msg38729.html + http://www.mail-archive.com/source-changes-full@n.../msg38729.html Sun Jun 10 12:43:23 2012 Tanaka Akira <akr@f...> @@ -12441,7 +12445,7 @@ Tue May 8 07:44:18 2012 NARUSE, Yui < https://github.com/ruby/ruby/blob/trunk/ChangeLog#L12445 * ext/openssl/ossl_ssl.c (ossl_start_ssl): remove useless rb_sys_fail before ossl_raise. this cause a test failure on Linux. - http://u64.rubyci.org/~chkbuild/ruby-trunk/log/20120507T190102Z.log.html.gz + http://u64.rubyci.org/~chkbuild/ruby-trunk/log/20120507T190102Z.log.html.gz Tue May 8 05:35:18 2012 Eric Hodel <drbrain@s...> @@ -12476,13 +12480,13 @@ Tue May 8 02:34:26 2012 NARUSE, Yui < https://github.com/ruby/ruby/blob/trunk/ChangeLog#L12480 * lib/minitest/unit.rb (assert_match): refix of r35563. r35563 breaks the intention of the original change. - https://github.com/seattlerb/minitest/commit/68858105b2eb11c85105ffac5f32b662c59397f3 + https://github.com/seattlerb/minitest/commit/68858105b2eb11c85105ffac5f32b662c59397f3 * lib/minitest/unit.rb (refute_match): ditto. Mon May 7 21:19:17 2012 NARUSE, Yui <naruse@r...> * ext/json: Merge JSON 1.7.1. - https://github.com/flori/json/commit/e5b9a9465c1159fae533bca320d950b772bcb4ac + https://github.com/flori/json/commit/e5b9a9465c1159fae533bca320d950b772bcb4ac Mon May 7 22:54:22 2012 Martin Bosslet <Martin.Bosslet@g...> @@ -12774,7 +12778,7 @@ Fri Apr 27 07:15:07 2012 Tanaka Akira https://github.com/ruby/ruby/blob/trunk/ChangeLog#L12778 * ext/socket/socket.c (sock_s_pack_sockaddr_un): support the longest path in sockaddr_un, really. reported by nagachika. - http://d.hatena.ne.jp/nagachika/20120426/ruby_trunk_changes_35474_35476 + http://d.hatena.ne.jp/nagachika/20120426/ruby_trunk_changes_35474_35476 Thu Apr 26 12:28:06 2012 Tanaka Akira <akr@f...> @@ -12939,7 +12943,7 @@ Mon Apr 23 20:11:02 2012 Tanaka Akira https://github.com/ruby/ruby/blob/trunk/ChangeLog#L12943 * lib/drb/ssl.rb: generate 1024 bits RSA key instead of 512 bits. OpenSSL 1.0.1 rejects 512 bits RSA key for TLS1.2 with SHA512. - http://rt.openssl.org/Ticket/Display.html?id=2769&user=guest&pass=guest + http://rt.openssl.org/Ticket/Display.html?id=2769&user=guest&pass=guest reported by Bohuslav Kabrda. [ruby-core:43844] [ruby-trunk - Bug #6221] @@ -13419,7 +13423,7 @@ Sun Apr 8 22:46:01 2012 NARUSE, Yui < https://github.com/ruby/ruby/blob/trunk/ChangeLog#L13423 * ext/json/generator/generator.c (generate_json_bignum): add RB_GC_GUARD. - http://fb.rubyci.org/~chkbuild/ruby-trunk/log/20120407T210301Z.diff.html.gz + http://fb.rubyci.org/~chkbuild/ruby-trunk/log/20120407T210301Z.diff.html.gz Sun Apr 8 07:26:40 2012 NARUSE, Yui <naruse@r...> @@ -20162,7 +20166,7 @@ Wed Aug 31 17:28:23 2011 Hiroshi Nakamu https://github.com/ruby/ruby/blob/trunk/ChangeLog#L20166 * variable.c (rb_autoload_load): Move RB_GC_GUARD() to proper position based on suggestion by CHIKANAGA Tomoyuki at - http://d.hatena.ne.jp/nagachika/20110826/ruby_trunk_changes_33070_33078 + http://d.hatena.ne.jp/nagachika/20110826/ruby_trunk_changes_33070_33078 * variable.c (autoload_defined_p): Fix incompatible autoload behavior that causes Rails crash. Class definition instruction defined in @@ -20365,8 +20369,8 @@ Sat Aug 27 11:18:12 2011 Hiroshi Nakamu https://github.com/ruby/ruby/blob/trunk/ChangeLog#L20369 * Revert r33078. It caused a Rails application NoMethodError. - /home/nahi/git/emptyApp/ruby/1.9.1/gems/rack-mount-0.6.14/lib/rack/mount/utils.rb:157: warning: toplevel constant ScanError referenced by Regin::Parser::ScanError - /home/nahi/git/emptyApp/ruby/1.9.1/gems/rack-mount-0.6.14/lib/rack/mount/vendor/regin/regin/parser.rb:17:in `parse_regexp': undefined method `scan_str' for #<Regin::Parser:0x00000002344548> (NoMethodError) + /home/nahi/git/emptyApp/ruby/1.9.1/gems/rack-mount-0.6.14/lib/rack/mount/utils.rb:157: warning: toplevel constant ScanError referenced by Regin::Parser::ScanError + /home/nahi/git/emptyApp/ruby/1.9.1/gems/rack-mount-0.6.14/lib/rack/mount/vendor/regin/regin/parser.rb:17:in `parse_regexp': undefined method `scan_str' for #<Regin::Parser:0x00000002344548> (NoMethodError) Sat Aug 27 08:44:58 2011 Eric Hodel <drbrain@s...> @@ -20960,7 +20964,7 @@ Sun Aug 7 00:42:55 2011 NARUSE, Yui < https://github.com/ruby/ruby/blob/trunk/ChangeLog#L20964 * ext/tk/lib/tk/wm.rb (Tk::Wm.command): Add the missing receiver before calling epath. patched by flori - https://github.com/flori/ruby/commit/aa9474d32e5f2c57f8b0e2e0c528a03f06a4d433 + https://github.com/flori/ruby/commit/aa9474d32e5f2c57f8b0e2e0c528a03f06a4d433 Sat Aug 6 07:06:34 2011 Eric Hodel <drbrain@s...> Index: lib/csv.rb =================================================================== --- lib/csv.rb (revision 38632) +++ lib/csv.rb (revision 38633) @@ -274,7 +274,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L274 # 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 @@ -293,6 +293,42 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L293 # # :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: # []=( header, value ) # []=( header, offset, value ) # []=( index, value ) Index: test/csv/test_row.rb =================================================================== --- test/csv/test_row.rb (revision 38632) +++ test/csv/test_row.rb (revision 38633) @@ -78,6 +78,33 @@ class TestCSV::Row < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_row.rb#L78 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) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/