From: Akinori MUSHA Date: 2008-04-21T20:24:22+09:00 Subject: [ruby-dev:34475] Re: Object#tap のサンプル --pgp-sign-Multipart_Mon_Apr_21_20:24:20_2008-1 Content-Type: text/plain; charset=ISO-2022-JP At Mon, 21 Apr 2008 17:30:26 +0900, okkez wrote: > 1.8 の HEAD で Object#tap のサンプルコードが Syntax Error になって動きません。 > > % ruby-1.8.x -v sample_tap.rb > ruby 1.8.7 (2008-04-20 patchlevel 5000) [i686-linux] > sample_tap.rb:4: syntax error, unexpected '.', expecting $end > .to_a .tap {|x| puts "array: #{x.inspect}"} > ^ > > % ruby-1.9.x -v sample_tap.rb > ruby 1.9.0 (2007-12-25 revision 0) [i686-linux] > original: 1..10 > array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > evens: [2, 4, 6, 8, 10] > squares: [4, 16, 36, 64, 100] > > trunk(r13777から) ではメソッド呼び出しのドットが前に付いていても動くようになった > という話を ruby-dev:32393 で見たので、この件が1.8にバックポートされないのなら > object.c の該当部分を直しておいた方がよいと思います。 > # メソッド呼び出しのドットを後ろに移すだけですが。。。 > > Index: object.c > =================================================================== > --- object.c (revision 16122) > +++ object.c (working copy) > @@ -502,10 +502,10 @@ > * The primary purpose of this method is to "tap into" a method chain, > * in order to perform operations on intermediate results within the chain. > * > - * (1..10) .tap {|x| puts "original: #{x.inspect}"} > - * .to_a .tap {|x| puts "array: #{x.inspect}"} > - * .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"} > - * .map { |x| x*x } .tap {|x| puts "squares: #{x.inspect}"} > + * (1..10) .tap {|x| puts "original: #{x.inspect}"}. > + * to_a .tap {|x| puts "array: #{x.inspect}"}. > + * select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"}. > + * map { |x| x*x } .tap {|x| puts "squares: #{x.inspect}"} > * > */  ご指摘ありがとうございます。平凡にこう直していいかも。 (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.tap { |x| puts "array: #{x.inspect}" }.select {|x| x%2==0}.tap { |x| puts "evens: #{x.inspect}" }.map {|x| x*x}.tap { |x| puts "squares: #{x.inspect}" } -- Akinori MUSHA / http://akinori.org/ --pgp-sign-Multipart_Mon_Apr_21_20:24:20_2008-1 Content-Type: application/pgp-signature Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (FreeBSD) iEYEABECAAYFAkgMeWQACgkQkgvvx5/Z4e7LAQCgpSXb8sy2X6CBCt7zbxKouoqu aMkAnjUHoixDL9ymJdcxYv74CUp+F0xs =Nud9 -----END PGP SIGNATURE----- --pgp-sign-Multipart_Mon_Apr_21_20:24:20_2008-1--